Search code examples
hibernatejpajava-ee-7

EntityNotFoundException while retrieving ManyToOne relation but entity id exists


When I execute query findeAtDate I got the error entityNotFoundException but in database I have the entity with id 4, so could it be some data error in the entity Work with 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;
    }
...

Work data

enter image description here

DeliveryNote data

enter image description here

Error

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

Solution

  • The problem is that there is an inconsistency between mandatory fields in database and mandatory properties of entity Work.

    In database the fields description and address are empty but the entity must receive them as mandatory, so when the manyToOne relation in DeliveryNote with Work try to eager load it produces an error because the fields are empty.

    So it receives the error message that some data error in the entity Work with id 4.