PDA

View Full Version : spring+hibernate+transaction HibernateSystemException


agile_boy
Dec 10th, 2004, 04:57 AM
1) testCreateFullNode(com.xxx.dao.spring.hibernate.Em sEquipmentServiceImplTest)org.springframework.orm. hibernate.HibernateSystemException: a different object with the same identifier value was already associated with the session: 1102663343965, of class: com.xxx.bo.resource.Node; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1102663343965, of class: com.xxx.bo.resource.Node
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1102663343965, of class: com.xxx.bo.resource.Node
at net.sf.hibernate.impl.SessionImpl.checkUniqueness( SessionImpl.java:1686)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable( SessionImpl.java:1452)
at net.sf.hibernate.impl.SessionImpl.doUpdate(Session Impl.java:1479)
at net.sf.hibernate.impl.SessionImpl.update(SessionIm pl.java:1364)
at org.springframework.orm.hibernate.HibernateTemplat e$16.doInHibernate(HibernateTemplate.java:409)
at org.springframework.orm.hibernate.HibernateTemplat e.execute(HibernateTemplate.java:228)
at org.springframework.orm.hibernate.HibernateTemplat e.update(HibernateTemplate.java:406)
at com.xxx.dao.spring.hibernate.BaseDaoHibernateImpl. updateObject(BaseDaoHibernateImpl.java:67)
at com.xxx.dao.spring.hibernate.EquipmentDaoHibernate Impl.createNode(EquipmentDaoHibernateImpl.java:81)
at com.xxx.service.impl.EmsEquipmentServiceImpl.creat eFullNode(EmsEquipmentServiceImpl.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:295)
at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:154)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :121)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:56)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :143)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy0.createFullNode(Unknown Source)
at com.xxx.dao.spring.hibernate.EmsEquipmentServiceIm plTest.testCreateFullNode(EmsEquipmentServiceImplT est.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at com.xxx.dao.spring.hibernate.EmsEquipmentServiceIm plTest.main(EmsEquipmentServiceImplTest.java:23)


the relative configuration is :

<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransac tionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

robh
Dec 10th, 2004, 07:42 AM
The exception statck trace shows that you are trying to insert an object with a duplicate key. Have you checked your code to make sure that this is not the case. From what you have posted I can't really tell what your problem is.

Rob

agile_boy
Dec 10th, 2004, 09:27 AM
thanks for Rob's attention.
i am sure that the key is not duplicate ,and my create object logic is:
if the object is exist,the update this object,others will create this object.please the the codes :
public interface EmsEquipmentService {
public List getAllNodes();
public Long createFullNode(FullNode fullNode);
}
public class EmsEquipmentServiceImpl implements EmsEquipmentService {
private EquipmentDao equipmentDao;
......
public Long createFullNode(FullNode fullNode) {
Long l = equipmentDao.createNode(fullNode.getNode());
Set set = fullNode.getShelves();
Iterator it = set.iterator();
for(;it.hasNext();) {
Shelf shelf = (Shelf)it.next();
equipmentDao.createShelf(shelf);
}
return l;
}
}


public class EquipmentDaoHibernateImpl extends BaseDaoHibernateImpl implements EquipmentDao {
.....
.....
public Long createNode(Node node) {
Node n = getNodeByNodeId(node.getNodeId());
if (n==null) {
return super.createObject(node);
}else {
node.setOid(n.getOid());
super.updateObject(node);
}
return n.getOid();
}
public Node getNodeByNodeId(NodeId nodeId) {
List list = super.getHibernateTemplate().find(
"from Node node " +
"where node.nodeId.emsId=? and node.nodeId.nodeId = ?",
new Object[]{new Integer(nodeId.getEmsId()),nodeId.getNodeId()},
new Type[] {Hibernate.INTEGER,Hibernate.STRING});
if (list.size()==0) {
return null;
}else if (list.size()!=1) {
throw new ObjectRetrievalFailureException(Node.class, nodeId,
" the size should be 1",new RuntimeException());
}

return (Node)list.get(0);
}

thanks.

agile_boy
Dec 10th, 2004, 09:57 AM
And if i remove the transaction management,then the error is gone.
So i think the problem is in transaction.

agile_boy
Dec 13th, 2004, 02:50 AM
the problem is resolved.
it is maybe a hibernate problem,in my way:using the saveOrUpdateCopy() method to replace the save()