用Hibernate的update时,多次执行回报这个错
a different object with the same identifier value was already
associated with the session:
因为同一个session里面有了两个相同标识但是是不同实体
update方法:
/**
* @param object
* @return
* @throws HibernateException
*/
public boolean saveOrUpdate(Object object) {
System.out
.println("=============== saveOrUpdate() start =================");
Session session = null;
Transaction tx = null;
logger.info("saveOrUpdate(Object) - start");
try {
session = SessionFactory.getSession();
tx = session.beginTransaction();
//session.refresh(object);
//session.clear();
session.saveOrUpdate(object);
tx.commit();
SessionFactory.closeSession();
logger.info("saveOrUpdate(Object) - end");
System.out
.println("============== saveOrUpdate() end =================");
return true;
} catch (HibernateException e) {
logger.error("saveOrUpdate(Object object) throw exception = ", e);
try {
if (tx != null)
tx.rollback();
} catch (HibernateException ex) {
throw new RuntimeException("tx.rollbacd() throw exception = "
+ ex.toString());
}
throw new RuntimeException(
"saveOrUpdate(Object) throw exception = " + e.toString());
} finally {
try {
if (session != null)
SessionFactory.closeSession();
} catch (HibernateException e) {
throw new RuntimeException(
"SessionFactory.closeSession() throw exception = "
+ e.toString());
}
}
}
后来在网上找到了3种办法:
(解决方法一:session.clean()
PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的
操作,有可能会报出"Found two representations of same collection"异常。
解决方法二:session.refresh(object)
PS:当object不是数据库中已有数据的对象的时候,不能使用
session.refresh(object)因为该方法是从hibernate的session中去重新取object
,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之
前还需要判断一下。
解决方法三:session.merge(object)
PS:Hibernate里面自带的方法,推荐使用。
)
自己都试了,发现我用session.merge(object) 一定报同样的错误;
session.refresh(object) 偶尔报同样的错误;
Found two representations of same collection这个错误没遇到。
只有用session.clean()的时候正常!
分享到:
相关推荐
The Digital Object Identifier initiative: metadata implications Context/status information The purpose of this document is to generate a DOI Foundation consensus paper on metadata, as a companion ...
- FIX: The value of some string flex-properties that began with a parenthese or curly bracket had no apostrophe at the end; that caused an error when reading. (fixed TPropList.SavePropValue for the ...
Another type of table lock is a schema stability lock (Sch-S) and is compatible with all table locks except the schema modification lock (Sch-M). The schema modification lock (Sch-M) is incompatible ...
In these systems, packets are transmitted over the same wireless infrastructure used for cellular telephony, with the base station thus being managed by a telecommunications provider. This provides ...
Each index row in node pages contains an index key (or set of keys for a composite index) and a pointer to a page at the next level for which the first key value is the same as the key value in the ...
- When the signal conditioning function "S&M" was used for a byte value, the driver wrote wrong values to the PLC. Build 223 : New Functions: - The number of supported NetLinks has been ...
When the SID is found in a value it is replaced with the new computer SID, and when the SID is found in a name, the key and its subkeys are copied to a new subkey that has the same name except with ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 ...
10.9. Replicating object between two different datastores 10.10. Flushing the Session 10.11. Transitive persistence 10.12. Using metadata 11. Transactions And Concurrency 11.1. Session and transaction...
上web tools课时老师发的关于hibernate的教程之一。大概内容看标题就知道了。需要注意的是文档全部是英文的。英文不好的童鞋就不要下了。。。
5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet 标记 5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助...
This was due to a problem where it would be freed automatically if there was a problem with the ArchiveStream when trying to open it as a zip file (possibly corrupt). Best practice is that ...
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...
5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet 标记 5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助...
A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource. FilterConfig - interface javax.servlet....
## to use the second-level cache with JTA, Hibernate must be able to obtain the JTA TransactionManager #hibernate.transaction.manager_lookup_class org.hibernate.transaction....
5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet 标记 5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库...
多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet 标记 5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库...
Hibernate参考文档 目录 前言 1. 翻译说明 2. 版权声明 1. 在Tomcat中快速上手 1.1. 开始Hibernate之旅 1.2. 第一个持久化类 1.3. 映射cat 1.4. 与Cat同乐 1.5. 结语 2. Hibernate入门 2.1. 前言 2.2. 第...