org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was

在使用hibernate框架里面的:saveOrUpdate报错:

意思就是另一个对象的id(id同值)已经被session关联了。

原因分析:

在第1步中中通过titleList.get(0)获取一个对象duty1,在第2步中将duty的dutyId设置成duty1的dutyId,而duty1本来就是存在一级缓存session里面的,在第3步时saveOrUpdate(duty)相当于内存中存在两个dutyId一样的对象,因此抛出异常。

解决方法:

如果dao或Service类继承了HibernateDaoSupport,可以调用this.getHibernateTemplate().clear();清除session(的关联)。

如图中的第二步添加了:this.getHibernateTemplate().clear();

时间: 2024-10-27 06:36:01

org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was的相关文章

org.springframework.dao.InvalidDataAccessApiUsageException: The given object has a null identifier:

JSP 页面保存操作报错: ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- exception org.springframework.dao.InvalidData

org.springframework.dao.DuplicateKeyException: 问题

org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [com.sinosoft.claim.schema.model.PrpLlossInfo#REG13552269089070]; nested exception is org.hibernate.NonUniqueObj

a different object with the same identifier value was already associated with the session

错误提示: org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [cn.itcast.bos.domain.User#4]; nested exception is org.hibernate.NonUniqueObjectException: a different obj

OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException

本文转载:Iuranus 报错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definit

The type org.springframework.dao.support.DaoSupport cannot be resolved. It is indirectly referenced

springmvc mybatis整合,遇到错误:The type org.springframework.dao.support.DaoSupport cannot be resolved. It is indirectly referenced 缺依赖包导致,在pom.xml加入依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>org.springframework.tr

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案

1.错误信息 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: sys.entity.Role at org.springframew

java.lang.NoSuchMethodError: org.springframework.dao.IncorrectResultSizeDataAccessException

spring data jpa  运用,在dao类中写自己新增的方法,使用@query写hql语句,出现以下异常: Caused by: java.lang.NoSuchMethodError: org.springframework.dao.IncorrectResultSizeDataAccessException.<init>(Ljava/lang/String;ILjava/lang/Throwable;)V at org.springframework.orm.jpa.EntityM

【异常】Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataIntegrityViolationException

Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataIntegrityViolationException at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(U

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibern