-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: org.hibernate.NonUniqueObjectException while saving records
PostPosted: Thu Mar 05, 2009 11:46 am 
Newbie

Joined: Thu Mar 05, 2009 11:17 am
Posts: 5
Hi All,

I have one problem with hibernate while saving one record to database using the save
function.While saving it is saying that
"org.hibernate.NonUniqueObjectException:a different object with the same
identifier value was already associated with the session:
[com.path.dcrms.entities.CrmTeams#2]"

This only when i am trying to delete one record and immediately
re-insert another record with the same id (ie: the same id of previously
deleted record).

I have searched so many forums and its saying use merge instead or save .
It is working fine for some insertions but in some cases the data is not getting
insering to the database ; but there is no exception.

Also i tried all possible alternatives of flush and evict and all.

If anybody has similar experience pls tell me the solution.

Here is my configuration of my project ...

I am using hibernate3.jar with glassfish server and my eclipse6.0 work bench for developement.

Here is my code between opensession and closes


HibernateSessionFactory.getSession().save(transientInstance);
HibernateSessionFactory.getSession().flush();
HibernateSessionFactory.getSession().evict(transientInstance);


javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:3869)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:3769)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:3571)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1354)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1316)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:210)
at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:117)
at $Proxy27.save(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:233)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:152)
at com.sun.corba.ee.impl.presentation.rmi.bcel.BCELStubBase.invoke(BCELStubBase.java:225)
at com.path.dcrms.daoInterface.__CrmTeamsFacadeRemote_Remote_DynamicStub.save(com/path/dcrms/daoInterface/__CrmTeamsFacadeRemote_Remote_DynamicStub.java)
at com.path.dcrms.daoInterface._CrmTeamsFacadeRemote_Wrapper.save(com/path/dcrms/daoInterface/_CrmTeamsFacadeRemote_Wrapper.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.path.dcrms.EJBOperations.RequestHandler.handle(RequestHandler.java:92)
at com.path.dcrms.team.TeamAction.saveTeamInfo(TeamAction.java:138)
at com.path.dcrms.team.TeamAction.execute(TeamAction.java:61)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:206)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:150)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:272)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:637)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:568)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.path.dcrms.entities.CrmTeams#2]
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:534)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:522)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at com.path.dcrms.dao.CrmTeamsFacade.save(CrmTeamsFacade.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1067)
at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:176)
at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2895)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:3986)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:203)
... 50 more
Mar 5, 2009 5:13:01 PM com.sun.enterprise.server.logging.SystemOutandErrHandler$LoggingByteArrayOutputStream flush


Thanks in Advance

Anish P Isaac


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 11:51 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Why would you remove an entity and after that try to insert it again? Thats not right.
You can use merge() to update the existing entity, but delete means it has to be deleted and not inserted within the same transaction.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: org.hibernate.NonUniqueObjectException
PostPosted: Thu Mar 05, 2009 12:17 pm 
Newbie

Joined: Thu Mar 05, 2009 11:17 am
Posts: 5
Hi mmerder,

Thanks for your reply.

deletetion and insertion are not in the same transaction . Please see my code for saving and deletion...

public void save(CrmTeams transientInstance) throws Exception{
ELogger.log("saving CrmTeams instance", DcrmsConstants.INFO);
try {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Long teamCode = 1L;
String sqlQuery = "select max(model.teamCode) from CrmTeams model";
Object obj = session.createQuery(sqlQuery).uniqueResult();
Date currentDate = Calendar.getInstance().getTime();
if(obj!=null)
teamCode = (((Long)obj)+1);
transientInstance.setTeamCode(teamCode);
transientInstance.setCreatedDt(currentDate);
for(CrmTeamDept crmTeamDept: transientInstance.getCrmTeamDepts()){
crmTeamDept.getId().setCrmTeams(transientInstance);
crmTeamDept.setCreatedDt(currentDate);
}
for(CrmTeamsBranches crmTeamBranches: transientInstance.getCrmTeamsBrancheses()){
crmTeamBranches.getId().setCrmTeams(transientInstance);
crmTeamBranches.setCreatedDt(currentDate);
}
for(CrmUserTeamDet crmUserTeamDet: transientInstance.getCrmUserTeamDets()){
crmUserTeamDet.getId().setCrmTeams(transientInstance);
crmUserTeamDet.setCreatedDt(currentDate);
}
// CrmTeams crmTeams = (CrmTeams)HibernateSessionFactory.getSession().merge(transientInstance);
//ELogger.log("save successful"+crmTeams.getTeamCode(), DcrmsConstants.INFO);
session.save(transientInstance);
session.flush();
session.evict(transientInstance);
ELogger.log("save successful", DcrmsConstants.INFO);
tx.commit();
session.close();
} catch (Exception ex) {
ELogger.log("save failed", DcrmsConstants.SEVERE);
throw ex;
}
}

public void delete(Vector<Long> values) throws Exception{
ELogger.log("deleting CrmTeams instance", DcrmsConstants.INFO);
try {
Session session = HibernateSessionFactory.getSession();
Transaction tx =session.beginTransaction();
for(Long teamId:values){
CrmTeams obj = (CrmTeams)HibernateSessionFactory.getSession().get(CrmTeams.class, teamId);
ELogger.log("deleting crmTeam with id"+teamId, DcrmsConstants.INFO);
session.delete(obj);
session.flush();
session.evict(obj);
ELogger.log("deleted crmTeam with id"+teamId, DcrmsConstants.INFO);
}
ELogger.log("delete process successful", DcrmsConstants.INFO);
tx.commit();
session.close();
} catch (Exception ex) {
ELogger.log("delete failed", DcrmsConstants.SEVERE);
throw ex;
}
}


Thanks and Regards,

Anish P isaac


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.