简体   繁体   English

使用count()时,命名参数中的JPA错误

[英]JPA error in named parameters wher using count()

Hi, I have this simple table in mysql: 嗨,我在mysql中有这个简单的表:

+---------------------------------+---------+------+-----+---------+----------------+
| Field                           | Type    | Null | Key | Default | Extra          |
+---------------------------------+---------+------+-----+---------+----------------+
| id_documento_referencia_importa | int(11) | NO   | PRI | NULL    | auto_increment |
| id_tipo_documento               | int(11) | NO   | MUL | NULL    |                |
| tipo                            | char(1) | NO   | MUL | NULL    |                |
| id_tipo_documento_origen        | int(11) | NO   | MUL | NULL    |                |
| estado_documento_origen_antes   | char(1) | YES  | MUL | NULL    |                |
| estado_documento_origen_despues | char(1) | YES  | MUL | NULL    |                |
+---------------------------------+---------+------+-----+---------+----------------+

and this entity: 和这个实体:

public class DocumentoReferenciaImporta implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "id_documento_referencia_importa")
  private Integer idDocumentoReferenciaImporta;
  @Basic(optional = false)
  @NotNull
  @Column(name = "tipo")
  private char tipo;
  @JoinColumn(name = "estado_documento_origen_despues", referencedColumnName = "estado_documento")
  @ManyToOne
  private EstadoDocumento estadoDocumentoOrigenDespues;
  @JoinColumn(name = "estado_documento_origen_antes", referencedColumnName = "estado_documento")
  @ManyToOne
  private EstadoDocumento estadoDocumentoOrigenAntes;
  @JoinColumn(name = "id_tipo_documento_origen", referencedColumnName = "id_tipo_documento")
  @ManyToOne(optional = false)
  private TipoDocumento idTipoDocumentoOrigen;
  @JoinColumn(name = "id_tipo_documento", referencedColumnName = "id_tipo_documento")
  @ManyToOne(optional = false)
  private TipoDocumento idTipoDocumento;
  [...snip...]

the table reference another table, id_tipo_documento, that is a simple id-name table. 该表引用了另一个表id_tipo_documento,它是一个简单的id-name表。 Somewhere in the code I have this JPA query, that is working perfectly: 在代码中的某个地方,我有这个JPA查询,它运行良好:

String jpql="SELECT o FROM DocumentoReferenciaImporta o "
        + "WHERE o.idTipoDocumentoOrigen.idTipoDocumento = :idTipoDocumento "
        + "AND o.idTipoDocumentoOrigen.idTipoDocumento = :idTipoDocumento2 "
        + "AND o.tipo = :tipo";
Query query = em.createQuery(jpql, DocumentoReferenciaImporta.class);
query.setParameter("idTipoDocumento",102);
query.setParameter("idTipoDocumento2", 103);
query.setParameter("tipo",'R');
query.getResultList();

In another function I need to count the records, so I do: 在另一个功能中,我需要对记录进行计数,因此我需要执行以下操作:

String jpql="SELECT COUNT(o) FROM DocumentoReferenciaImporta o "
        + "WHERE o.idTipoDocumentoOrigen.idTipoDocumento = :idTipoDocumento "
        + "AND o.idTipoDocumentoOrigen.idTipoDocumento = :idTipoDocumento2 "
        + "AND o.tipo = :tipo";
Query query = em.createQuery(jpql, DocumentoReferenciaImporta.class);
query.setParameter("idTipoDocumento",102);
query.setParameter("idTipoDocumento2", 103);
query.setParameter("tipo",'R');
query.getResultList();
int count = ((Integer)q.getSingleResult()).intValue();

this query, instead, give me an error like: 相反,此查询给我一个错误,例如:

java.lang.IllegalStateException: Query argument idTipoDocumento2 not found in the list of parameters provided during query execution.

Additionally every application deployment result in a different error: sometimes the argument causing the error is idTipoDocumento2, sometimes is idTipoDocumento, sometimes is tipo. 此外,每个应用程序部署都会导致不同的错误:有时,导致错误的参数是idTipoDocumento2,有时是idTipoDocumento,有时是tipo。 I am doing something wrong or there's a problem with JPA an COUNT? 我做错了,或者JPA COUNT有问题?

Thanks! 谢谢!

Maybe there is also something else wrong, but at least there is following problems: 也许还存在其他问题,但是至少存在以下问题:

Following is never true, because value cannot be both 102 and 103; 跟随永远都不是正确的,因为值不能既是102又是103。

o.idTipoDocumentoOrigen.idTipoDocumento = :idTipoDocumento //parameter 102
AND o.idTipoDocumentoOrigen.idTipoDocumento = :idTipoDocumento2 //parameter 103

COUNT function in JPQL returns Long. JPQL中的COUNT函数返回Long。 That's why second argument to createQuery should be Long.class. 这就是为什么createQuery的第二个参数应该是Long.class。

Query query = em.createQuery(jpql, Long.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM