spring Hibernate多对一映射中的EntityNotFoundException但是数据存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13539050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EntityNotFoundException in Hibernate Many To One mapping however data exist
提问by Badal Singh
I am getting javax.persistence.EntityNotFoundException error when I am trying to get User through Invoice object
当我尝试通过 Invoice 对象获取 User 时出现 javax.persistence.EntityNotFoundException 错误
invoice.getUser().getId()
发票.getUser().getId()
Error is as follows
错误如下
javax.persistence.EntityNotFoundException: Unable to find com.indianretailshop.domain.User with id 5
at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:137)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:189)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:178)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
Entity classes are as follows(getters and setters are not included)
实体类如下(不包括getter和setter)
@Entity
@Table(name="users")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int id;
.
.
.
//bi-directional many-to-one association to Invoice
@OneToMany(mappedBy="user")
private List<Invoice> invoices;
}
@Entity
@Table(name="invoice")
public class Invoice implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int id;
.
.
.
//bi-directional many-to-one association to User
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Users_id")
private User user;
}
采纳答案by rajesh kakawat
The problem could be that the direct entity does not exist, but also it could be that the referenced entity from that entity, normally for a EAGER fetch type, or optional=false.
问题可能是直接实体不存在,但也可能是来自该实体的引用实体,通常用于 EAGER 获取类型,或 optional=false。
Try this:
尝试这个:
//bi-directional many-to-one association to User
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Users_id")
private User user = new User();
回答by ozeray
I had the same problem, and
我遇到了同样的问题,并且
@NotFound(action = NotFoundAction.IGNORE)
solved my problem.
解决了我的问题。
回答by shane lee
If you use @ManyToOne, the referenced entity must exist. The only other option is to specify that field as a long and retrieve the referenced entity by means of a separate query.
如果使用@ManyToOne,则引用的实体必须存在。唯一的其他选项是将该字段指定为 long 并通过单独的查询检索引用的实体。
Throws an exception (javax.persistence.EntityNotFoundException) instead of returning null if it can't find the requested entity.
如果找不到请求的实体,则抛出异常 (javax.persistence.EntityNotFoundException) 而不是返回 null。
Use @NotFound annotation to resolve this exception if you are lazy loading and not handling this exception manually.
如果您是延迟加载且未手动处理此异常,请使用 @NotFound 注释来解决此异常。
@ManyToOne(
fetch = FetchType.LAZY)
@NotFound(
action = NotFoundAction.IGNORE)
@JoinColumn(
name = COLUMN,
referencedColumnName = COLUMN,
insertable = false,
updatable = false)
private Table table;
回答by user3450922
Not sure if that applies to your case.
不确定这是否适用于您的情况。
But I had a similar issue where I updated directly in the database table X and set a field null, but in java class the field was @NotNull.
但是我有一个类似的问题,我直接在数据库表 X 中更新并设置了一个字段为空,但在 java 类中该字段是@NotNull。
So when another class Y had a X object with ManyToOne, it wasn't able to load the entity because of the null value in the entity.
因此,当另一个类 Y 有一个带有 ManyToOne 的 X 对象时,由于实体中的空值,它无法加载实体。
回答by Haroon
please try the following
请尝试以下操作
@OneToMany(mappedBy="yourMappingattributeName",cascade=CascadeType.ALL)or
@OneToMany(mappedBy="yourMappingattributeName",cascade=CascadeType.ALL)或者
@OneToMany(mappedBy="yourMappingattributeName",cascade=CascadeType.MERGE)
回答by sendon1982
I had the similar issue recently, but the problem is that record is always existed in DB. So I did some investigation, found out that at some point that cached entity is marked as removal and reload failed before reloading from DB. It happened in Hibernate 4.3.5Final for me
Then I upgrade to Hibernate 4.3.11Final which seems to fix the issue.
我最近遇到了类似的问题,但问题是记录始终存在于数据库中。所以我做了一些调查,发现在某些时候缓存的实体被标记为删除并且在从数据库重新加载之前重新加载失败。它发生在Hibernate 4.3.5我的 Final 然后我升级到Hibernate 4.3.11Final 这似乎解决了这个问题。
回答by Shahid Hussain Abbasi
Use @NotFound(action = NotFoundAction.IGNORE) in case it's parent not exist
使用 @NotFound(action = NotFoundAction.IGNORE) 以防它的父级不存在
By using cascade=CascadeType.ALL it will delete it's parent entity
通过使用 cascade=CascadeType.ALL 它将删除它的父实体
回答by Khasan 24-7
Maybe one comes to this answer and finds it useful: In my case, I have marked my entity as deleted and entity who is relationship with this entity could not find it. So, changing deleted to false worked for me.
也许有人来到这个答案并发现它很有用:就我而言,我已将我的实体标记为已删除,并且与该实体有关系的实体找不到它。因此,将删除更改为 false 对我有用。

