java javax.persistence.EntityNotFoundException:无法找到 ID 为 X 的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28862986/
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
javax.persistence.EntityNotFoundException: Unable to find object with id X
提问by prabello
I'm trying to persist an object, after that i want to add 2 lists to that object and then update it (Since i can't persist the object with the lists).
我正在尝试持久化一个对象,之后我想向该对象添加 2 个列表,然后更新它(因为我无法用列表持久化该对象)。
All beign done inside a loop, the first iteration works just fine, from the second i get a EntityNotFoundException
saying that the ID wasn't found to do the update.
一切都在一个循环内完成,第一次迭代工作得很好,从第二次开始,我得到一个EntityNotFoundException
说没有找到 ID 来进行更新的说法。
private Foo foo;
private FooDao dao;
for(int i = 0 ; i<10 ; i++){
foo = new Foo();
foo.setVar(i);
dao.save(foo);
generateLists(); //creates a new list every interaction
foo.setCatList(catList);
foo.setBarList(barList);
dao.update(foo);
}
If i remove the Lists and the update it works fine.
如果我删除列表和更新它工作正常。
The object:
物体:
@Entity
public class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
private Integer id;
@Basic(optional = false)
@NotNull
private String var;
@OneToMany(cascade = CascadeType.ALL)
private List<Bar> barList;
@OneToMany(cascade = CascadeType.ALL)
private List<Cat> catList;
//Getters and Setters
}
The DAO methods:
DAO 方法:
public void save(Foo foo) {
this.manager.joinTransaction();
this.manager.persist(foo);
//this.manager.flush(); Tried this, but didn't work
}
public void update(Foo foo) {
this.manager.joinTransaction();
this.manager.merge(foo);
}
The error:
错误:
ERROR [org.jboss.as.ejb3] (EJB default - 1) javax.ejb.EJBTransactionRolledbackException: Unable to find foo with id 4
ERROR [org.jboss.as.ejb3.invocation] (EJB default - 1) JBAS014134: EJB Invocation failed on component FooDao for method public void FooDao.atualiza(Foo): javax.ejb.EJBTransactionRolledbackException: Unable to find Foo with id 4
ps: Using this generic approach to simplify, if needed i'll post my solution(or the mess that i call solution)
ps:使用这种通用方法来简化,如果需要,我会发布我的解决方案(或我称之为解决方案的混乱)
采纳答案by silentprogrammer
Because you are adding same lists again and again.Since you have OneToMany the second transaction says you already persisted that list.
因为您一次又一次地添加相同的列表。由于您有 OneToMany,第二个事务表示您已经保留了该列表。
An workaround would to change relation to ManyToMany
一种解决方法是改变与多对多的关系
回答by Shahid Hussain Abbasi
Adding @NotFound(action = NotFoundAction.IGNORE) with @ManyToOne or @OneToOne is helpful because ..ToOne is FetchType.EAGER by default
添加 @NotFound(action = NotFoundAction.IGNORE) 和 @ManyToOne 或 @OneToOne 很有帮助,因为 ..ToOne 默认是 FetchType.EAGER
回答by Koitoer
I believe problem is with the transactions obtain the transaction and commit within the save method in your dAO.
我相信问题在于交易获取交易并在您的 dAO 的保存方法中提交。
this.manager.getTransaction().commit();
If an error comes it is because there is no active transaction it is why the flush fails. Be sure you are within a transaction, consider use EntityTransaction
to begin , commit and end the transaction around your DAO methods.
如果出现错误,那是因为没有活动事务,这就是刷新失败的原因。确保您在事务中,考虑使用EntityTransaction
围绕您的 DAO 方法开始、提交和结束事务。
If you don't have an active TX you won't be able to write entities to the database, and when you try to merge something the entity won't be found then this is the reason for what you are getting EntityNotFoundException
如果您没有活动的 TX,您将无法将实体写入数据库,并且当您尝试合并某些内容时,将找不到实体,这就是您得到的原因 EntityNotFoundException
--- UPDATE
- - 更新
When you do the merge, what is the ID that the Foo object have ? I believe is not the correct one , or at least the last id inserted in the database, so what I suggest is find first and then merge.
进行合并时,Foo 对象的 ID 是多少?我相信这不是正确的,或者至少是插入数据库中的最后一个 id,所以我建议先找到然后合并。
Use
利用
<T> T find(java.lang.Class<T> entityClass,
java.lang.Object primaryKey)
Using the id, and verify the instance have the correct key
使用 id,并验证实例具有正确的密钥