7

我正在尝试查询 JPA 上的一些数据,这是我的查询:

@Override
    public List<HorarioAtencion> findByPeriodo(Person person, int dia, int mes, int anno) {
        Query busqueda = em.createNativeQuery("Select * from HorariosAtencion where " +
                                              "idPerson = ?1 AND "+
                                              "DAY( fechaInicio) <= ?2 AND MONTH( fechaInicio ) <= ?3 AND YEAR( fechaInicio) <= ?4 AND "+
                                              "DAY( fechaFin) >= ?2 AND MONTH( fechaFin ) >= ?3 AND YEAR( fechaFin ) >= ?4 ORDER BY TIME(fechaInicio)", HorarioAtencion.class);
       busqueda.setParameter(1,person.getId());
       busqueda.setParameter(2, dia);
       busqueda.setParameter(3, mes);
       busqueda.setParameter(4, anno);
       return busqueda.getResultList();
    }

此查询返回结果,但会触发下一个异常:

Caused by: Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [ArrayRecord(
     => 1
     => 13
     => 2013-01-04 12:25:00.0
     => 2013-01-04 12:25:00.0
     => true
     => true
     => true
     => true
     => true
     => true
     => true)] during the execution of the query was detected to be null.  Primary keys must not contain null.
Query: ReadAllQuery(referenceClass=HorarioAtencion sql="Select * from HorariosAtencion where idPerson = ? AND DAY( fechaInicio) <= ? AND MONTH( fechaInicio ) <= ? AND YEAR( fechaInicio) <= ? AND DAY( fechaFin) >= ? AND MONTH( fechaFin ) >= ? AND YEAR( fechaFin ) >= ? ORDER BY TIME(fechaInicio)")

这是如何引起的,我该如何解决?

实体类的列定义:

private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Basic(optional = false)
    @Column(name = "fechaInicio")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaInicio;

    @Basic(optional = false)
    @Column(name = "fechaFin")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaFin;

    @Basic(optional = false)
    @Column(name = "domingo")
    private boolean domingo;

    @Basic(optional = false)
    @Column(name = "lunes")
    private boolean lunes;

    @Basic(optional = false)
    @Column(name = "martes")
    private boolean martes;

    @Basic(optional = false)
    @Column(name = "miercoles")
    private boolean miercoles;

    @Basic(optional = false)
    @Column(name = "jueves")
    private boolean jueves;

    @Basic(optional = false)
    @Column(name = "viernes")
    private boolean viernes;

    @Basic(optional = false)
    @Column(name = "sabado")
    private boolean sabado;

    @JoinColumn(name = "idPersona", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Persona idPersona;
4

7 回答 7

14

您使用的是什么数据库,它为列名返回什么?

默认情况下, EclipseLink在较新的版本中区分大小写,除非将eclipselink.jpa.uppercase-column-names持久属性设置为 true。

"ID"如果数据库在您将列名定义为小写时将其返回为大写,这可能是本机查询的问题"id"

尝试更改注释中的列定义以匹配数据库使用的内容或添加值为 true 的属性。

于 2013-01-07T04:44:01.053 回答
5

问题是 eclipselink 区分大小写。有一个“提示”可以解决这个问题。在 persistence.xml 文件中的 properties 元素之间放置以下内容:

<!-- Hint to solve "The primary key read from the row [DatabaseRecord(...)] during the execution of the query was detected to be null." errors.-->
<property name="eclipselink.jpa.uppercase-column-names" value="true"/>

这将以不区分大小写的方式处理列名并消除错误。

于 2013-11-18T18:09:54.887 回答
2

找到这个线程https://forums.oracle.com/thread/614380

postgres 和 EclipseLink 也有同样的问题。

解决方法:将主键列改为大写即:

选择 id作为“ID”,field1, field2,..., fieldn from table ....

于 2013-07-13T01:53:51.197 回答
1

HorariosAtencion在表中的某处,在null与主键对应的字段中找到了一个值。

于 2013-01-04T19:47:43.213 回答
0

要么在 HorariosAtencion /Persona 的一个/多个主键上有空值。仔细检查您的数据库。

或者您需要设置角色ID而不是引用对象,请尝试

public List<HorarioAtencion> findByPeriodo(Person person, int dia, int mes, int anno) {
    Query busqueda = em.createQuery("from HorariosAtencion where " +
                                          "idPerson = ?1 AND "+
                                          "DAY( fechaInicio) <= ?2 AND MONTH( fechaInicio ) <= ?3 AND YEAR( fechaInicio) <= ?4 AND "+
                                          "DAY( fechaFin) >= ?2 AND MONTH( fechaFin ) >= ?3 AND YEAR( fechaFin ) >= ?4 ORDER BY TIME(fechaInicio)", HorarioAtencion.class);
   busqueda.setParameter(1,person);
   busqueda.setParameter(2, dia);
   busqueda.setParameter(3, mes);
   busqueda.setParameter(4, anno);
   return busqueda.getResultList();
}

或者

public List<HorarioAtencion> findByPeriodo(Person person, int dia, int mes, int anno) {
    Query busqueda = em.createNativeQuery("Select * from HorariosAtencion where " +
                                          "**idPerson.id** = ?1 AND "+
                                          "DAY( fechaInicio) <= ?2 AND MONTH( fechaInicio ) <= ?3 AND YEAR( fechaInicio) <= ?4 AND "+
                                          "DAY( fechaFin) >= ?2 AND MONTH( fechaFin ) >= ?3 AND YEAR( fechaFin ) >= ?4 ORDER BY TIME(fechaInicio)", HorarioAtencion.class);
   busqueda.setParameter(1,person.getId());
   busqueda.setParameter(2, dia);
   busqueda.setParameter(3, mes);
   busqueda.setParameter(4, anno);
   return busqueda.getResultList();
}
于 2013-02-17T19:23:02.817 回答
0

我遇到了同样的问题,我找到了解决方案,尝试从 em.createNativeQuery 行中删除“HorarioAtencion.class”。

于 2014-07-23T16:25:51.237 回答
0

...从行 [ArrayRecord(...

表示HorariosAtencion记录正常。有两个 ID、两个日期和所有布尔值。1可能是 ID 值。所以,我怀疑异常是指相关 Persona实体。是否有Persona带有空 PK 的 s?

于 2013-01-04T20:04:25.237 回答