首页 > 解决方案 > 检索 ManyToOne 关系但实体 ID 存在时出现 EntityNotFoundException

问题描述

当我执行查询 findeAtDate 时出现错误 entityNotFoundException 但在数据库中我有 id 为 4 的实体,那么在使用 id 4 的实体中可能是一些数据错误吗?

在此处输入图像描述

工作.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;
    }
...

工作数据

在此处输入图像描述

DeliveryNote 数据

在此处输入图像描述

错误

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
...

标签: hibernatejpajava-ee-7

解决方案


问题是数据库中的必填字段与实体 Work 的必填属性之间存在不一致。

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

所以它收到错误消息,实体中的一些数据错误使用 id 4 工作。


推荐阅读