java 使用休眠保存/更新对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11934944/
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
Saving/updating object with hibernate
提问by Jaanus
My class Foo has method:
我的班级 Foo 有方法:
protected void saveMany(Collection<T> many) {
for(Object one : many) {
one = session.merge(one); // also tried this commented out
session.saveOrUpdate(one); // session.merge(one);
}
Tried to use saveOrUpdate and also merge, but both gave me this exception:
尝试使用 saveOrUpdate 并合并,但两者都给了我这个例外:
Error: Invocation of method 'saveMany' in class Foo threw exception class org.hibernate.HibernateException : org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [Bar#581301]
How to fix this?
如何解决这个问题?
Side note:
边注:
When I truncate my table, the saving part works, but when I have the table populated, and run this method again, thus updating the table, it fails, with this exception
当我截断我的表格时,保存部分有效,但是当我填充表格并再次运行此方法,从而更新表格时,它会失败,但出现此异常
回答by Brad
If you have a detached instance of a Class with @Id
that already exists in the Hibernate Session cache, then you will have to call merge()
to merge the detached instance into the cache. Then you should call saveOrUpdate()
.
如果您有一个@Id
已在 Hibernate 会话缓存中存在的类的分离实例,那么您将必须调用merge()
以将分离的实例合并到缓存中。那么你应该调用saveOrUpdate()
.
But you have to be careful to persist the instance returnedfrom the call to merge() and not the instance you passed in as a parameter. So you code has to look like this
但是您必须小心保留从调用 merge()返回的实例,而不是作为参数传入的实例。所以你的代码必须看起来像这样
mergedOne = session.merge(one);
session.saveOrUpdate(mergedOne);
If you're operating inside a transaction the database will not be updated until the session ends and the transaction commits, or you explicitly call flush()
如果您在事务内部操作,则在会话结束并且事务提交之前数据库不会更新,或者您显式调用 flush()
回答by SJuan76
It says that you are trying to save a new object with an Id already used by another one.
它表示您正在尝试使用已被另一个对象使用的 ID 保存新对象。
Probably, you do not have properly implemented equals
and hashCode
. So, hibernate finds that you want to save Object2 with id1, and in the database there is Object2 with id1, but not Object2.equals(Object1)
可能,您没有正确实施equals
和hashCode
。所以,hibernate发现你要保存带有id1的Object2,而数据库中存在带有id1的Object2,但是没有Object2.equals(Object1)
回答by Grim
You have multiple versions of "one" in the cache of your Hibernatesessions!
您的 Hibernatesessions 缓存中有多个版本的“一个”!
You must must find the instances of "one" and remove the cache-information from the Hibernatesessions using ".evict(...)".
您必须找到“one”的实例并使用“.evict(...)”从 Hibernatesessions 中删除缓存信息。
Finally you can call saveOrUpdate succesuflly!
最后,您可以成功调用 saveOrUpdate!
回答by danny.lesnik
You are probably trying to Save List<T>
which has one or more equals objects.
您可能正在尝试 Save List<T>
,其中包含一个或多个 equals 对象。
Use SaveOrUpdate or merge to solve it or simpply try to make all objects unique.
使用 SaveOrUpdate 或 merge 来解决它或简单地尝试使所有对象都是唯一的。