Hi All,
I am facing an exception in hibernate while saving one record to database using save function. While saving it is throwing "org.hibernate.NonUniqueObjectException:a different object with the same
identifier value was already associated with the session: [com.path.dcrms.entities.CrmTeams#2]"
This happens when I am trying to delete an object from database and then trying to create another record with the same id (the ID which is the primary key of the table is created by taking the max of the id and adding one to it). The save and delete are happening as two different operations so its in different transactions.I have doubt that the memory is not getting refreshed.
I have searched in many forums and found that many are facing the same issue and there is no clear solution provided by any. Some forums suggested to use merge instead or save .
Merge is working and there is no exceptions, but the issue with merge is some time its not inserting the records to Database
Also tried all possible alternatives of flush and evict still its the same.
If anyone knows the solution for this please help.
Here is my configuration of my project ...
I am using hibernate3.jar with glassfish server and myeclipse6.0 work bench for developement with oracle 10g.
Here is my code for save and delete
// Save Method
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);
}
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;
}
}
// Delete Method
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;
}
}
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
Rajin Das
|