用hibernate做修改和删除的时候,有时候会遇到 org.hibernate.NonUniqueObjectException 异常,
a different object with the same identifier value was already associated with the session
可以解释为 有一个具有相同值的不同对象已经与和 session 相关联。
1. Session session = HibernateSessionFactory.getSession();
2.
3. // 加载OID为1L的对象,会被放在session缓存中
4. LoanDetail detail = (LoanDetail)session.get(LoanDetail.class,1L);
5.
6. // new 一个OID也为1L的临时对象
7. LoanDetail newDetail = new LoanDetail(1L);
8. ewDetail.setSubjectId(1000L);
9.
10. // 持久化一个临时对象,试图放在session的缓存中,因OID冲突出现异常
11. session.save(newDetail);
12.
13. // 执行saveOrUpdate同样会出现以上的异常
14. // session.saveOrUpdate(newDetail);
解决方法:
1) 如果用的 hibernate 2, 需要在get/load/query到持久化对象,赋上新的属性值,再 save/update/saveOrupdate.
对以上代码就是:不能 new 一个session中已存在OID的对象,直接
detail.setSubjectId(1000L);
session.save(detail);
session.save()一个持久化对象时,会转化成update调用。
2) 使用 hibernate 3 的 merge 方法. session.merge(newDetail)即可,它会在 session 缓存中找到持久化对象,把新对象的属性赋过去,再保存原session中的持久化对象。
如果在session或数据库中没有的对象,用merge方法的话,它也能够帮你把记录 insert 到表中,相当于 save 方法。
分享到:
相关推荐
weblogic12 下最新 org.hibernate.hql.ast.HqlToken解决办法
No Hibernate Session bound to thread,没有绑定线程解决办法,找了好久才找到的
org.hibernate.jpa.QueryHints jar hibernate-entitymanager-4.3.0.Final.jar
问题描述 用SpringBoot + Spring Data JPA操作数据库 项目启动的时候 报了一个错 SpringBoot的版本是2.2.6.RELEASE org.springframework.beans.factory.BeanCreationException: Error creating bean with name '...
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.service.internal....
Class'org.springframework.orm.hibernate3.LocalSessionFactoryBean'not found 解决办法
NULL 博文链接:https://hw1287789687.iteye.com/blog/1967701
org.hibernate.eclipse.feature_3.2.3.GAorg.hibernate.eclipse.feature_3.2.3.GA
jar包,官方版本,自测可用
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory 3.3配置如下: <property name="hibernate.cache.use_second_level_cache">true <property name="cache...
NULL 博文链接:https://hw1287789687.iteye.com/blog/1947557
osgi下web开发hibernate库,可以在virgo jetty上使用。
osgi开发中的hibernate库,可以从springsource上下载。适用于virgo服务器。
在eclipse上通过hibernate...重启eclipse即可生效(插件包所在路径是:D:\eclipse\plugins\org.hibernate.eclipse.libs_3.6.0.Final-v20130327-1513-B111\lib\tools\hibernate-tools-3.4.0.CR2.jar(版本号可能有出入))
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.connection.C3P0ConnectionProvider] as strategy [org.hibernate.engine.jdbc.... ...
包含hibernate所有所需jar包还有一些其他包日志包、jpa支持包等: 列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0....
hibernate简单的demo,编程语言——Java,框架——Spring,框架——Hibernate
org.hibernate.cfg This package defines APIs for configuring Hibernate, and classes for building the Hibernate configuration-time metamodel. org.hibernate.classic This package implements backward-...
org.hibernate.cfg This package defines APIs for configuring Hibernate, and classes for building the Hibernate configuration-time metamodel. org.hibernate.classic This package implements backward-...
NULL 博文链接:https://xiongjiajia.iteye.com/blog/1947270