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

EntityNotFoundException while retrieving ManyToOne relation but entity id exists

当我执行查询 findeAtDate 时,我收到错误 entityNotFoundException 但在数据库中我有 ID 为 4 的实体,所以它可能是实体 Work with id 4 中的一些数据错误?

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

工作数据

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

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

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

因此它收到错误消息,指出实体 Work with id 4 中的某些数据错误。