在检索ManyToOne关系时出现EntityNotFoundException但实体ID存在

时间:2018-08-26 21:48:36

标签: hibernate jpa java-ee-7

当我执行查询findeAtDate时,我得到错误errorEntityNotFoundException,但是在数据库中我有ID为4的实体,那么在ID为4的实体中可能会出现数据错误吗?

enter image description here

Work.java

@Entity
@Table(name = "t_work")
public class Work extends BaseTraceEntity {

    private static final long serialVersionUID = 1L;

    @Pattern(regexp = "[OR]\\d{3}\\/\\d{4}")
    @Basic(optional = false)
    @NotNull
    @Size(min = 9, max = 9)
    @Column(unique = true, nullable = false, length = 9)
    private String code;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(nullable = false, length = 45)
    private String name;

    @Basic(optional = false)
    @Column(length = 255)
    private String description;

    @Basic(optional = false)
    @Column(length = 100)
    private String address;

    @XmlJavaTypeAdapter(value = LocalDateAdapter.class)
    @NotNull
    @Column(name = "init")
    private LocalDate init;

    @XmlJavaTypeAdapter(value = LocalDateAdapter.class)
    @Column(name = "end")
    private LocalDate end;

    @ManyToOne(optional = false)
    @NotNull
    @JoinColumn(name = "idWorkType", referencedColumnName = "id", nullable = false)
    private WorkType type;

    @ManyToOne
    @JoinColumn(name = "idCompany", referencedColumnName = "id", nullable = false)
    private Company company;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "work", orphanRemoval = true)
    private Collection<Order_> orders;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "work", orphanRemoval = true)
    private Collection<DeliveryNote> deliveryNotes;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "work", orphanRemoval = true)
    private Collection<Task> tasks;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "work", orphanRemoval = true)
    private Collection<WorkContact> contacts;
...

DeliveryNote.java

@Entity
@Table(name = "t_deliveryNote")
@NamedQueries({
    @NamedQuery(name = DeliveryNote.findAtDate, query = "SELECT dn FROM DeliveryNote dn WHERE :date = :date AND dn.date_d is null")
})
public class DeliveryNote extends BaseTraceEntity {
...
@ManyToOne(optional = false)
    @XmlJavaTypeAdapter(value = WorkAdapter.class)
    @JoinColumn(name = "idWork", referencedColumnName = "id", nullable = false,
            foreignKey = @ForeignKey(name = "deliveryNote2work"))
    private Work work;
...

DeliveryNoteManager.java

@Stateless
public class DeliveryNoteManager {
...
    public List<DeliveryNote> findByAtDate(LocalDate date) {
        List<DeliveryNote> delNotes = this.em.createNamedQuery(DeliveryNote.findAtDate, DeliveryNote.class).
                setParameter("date", date).
                getResultList();

        return delNotes;
    }
...

工作数据

enter image description here

交货单数据

enter image description here

错误

23:40:46,607 ERROR [org.jboss.as.ejb3.invocation] (default task-104) WFLYEJB0034: EJB Invocation failed on component DeliveryNoteManager for method public java.util.List es.roscam.light.business.work.boundary.DeliveryNoteManager.findByAtDate(java.time.LocalDate): javax.ejb.EJBException: javax.persistence.EntityNotFoundException: Unable to find es.roscam.light.business.work.entity.Work with id 4
...
Caused by: javax.persistence.EntityNotFoundException: Unable to find es.roscam.light.business.work.entity.Work with id 4
...

1 个答案:

答案 0 :(得分:0)

问题在于数据库中的强制字段与实体Work的强制属性之间存在不一致。

在数据库中,字段描述和地址为空,但是实体必须强制接收它们,因此,当带有Work的DeliveryNote中的manyToOne关系试图急于加载时,由于字段为空,会产生错误。

因此它会收到错误消息,表明实体中的某些数据错误工作于ID 4。