hibernate - 检索 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
...
解决方案
问题是数据库中的必填字段与实体 Work 的必填属性之间存在不一致。
在数据库中,字段描述和地址是空的,但实体必须将它们作为强制性接收,因此当 DeliveryNote 与 Work 中的 manyToOne 关系尝试预先加载时,由于字段为空,它会产生错误。
所以它收到错误消息,实体中的一些数据错误使用 id 4 工作。
推荐阅读
- php - 如何编辑代码以允许用户删除折扣代码
- c# - 如何在c#中复制文件时获取文件名
- python - 如何提高循环遍历 numpy 线性求解的计算效率
- wordpress - 如何获取用于产品变体的 woocommerce 属性?
- java - 为服务器上的文件生成下载链接
- java - CompletableFuture - 运行多个 rest 调用抛出 java.util.concurrent.CompletionException
- java - android.speech.tts.TextToSpeech.setLanguage(java.util.Locale)' 在空对象引用上
- python - 如何获取数组中搜索元素的索引并更新该行Python中的最后一个元素
- itext - 使用 iText 在现有 pdf 的每一页中添加 TextField
- python - 提高 U-Net 分割模型的训练精度