Hibernate 检索多个同一关系时EntityNotFoundException,但存在实体id
当我执行查询FindeAttDate时,我得到了错误entityNotFoundException,但在数据库中,我有id为4的实体,所以它可能是id为4的实体工作中的某些数据错误吗 Work.javaHibernate 检索多个同一关系时EntityNotFoundException,但存在实体id,hibernate,jpa,java-ee-7,Hibernate,Jpa,Java Ee 7,当我执行查询FindeAttDate时,我得到了错误entityNotFoundException,但在数据库中,我有id为4的实体,所以它可能是id为4的实体工作中的某些数据错误吗 Work.java @Entity @Table(name = "t_work") public class Work extends BaseTraceEntity { private static final long serialVersionUID = 1L; @Pattern(reg
@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;
...
@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;
...
@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;
}
...
DeliveryNoteManager.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;
...
@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;
...
@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与Work中的manyToOne关系尝试立即加载时,会产生错误,因为字段为空
因此,它会收到错误消息,即实体中的某些数据错误使用id 4。您能否共享您的实体设计和示例数据?您是否也需要完整的deliveryNote设计?ypur WorkEntity中的id字段在哪里?它在BaseTraceEntity extends BaseEntity中。感谢您将解决方案发回。我希望每个人都能像你一样