I'm writing a web application that lets a user add employees to a database and then edit them. The users can add employees to the database, however when they try to update the employees, this error is thrown:
Code:
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [scheduler.Employee#93]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [scheduler.Employee#93]
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [scheduler.Employee#93]
at org.hibernate.engine.PersistenceContext.checkUniqueness(PersistenceContext.java:586)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:254)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:214)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
at org.hibernate.engine.Cascades.cascade(Cascades.java:819)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:316)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:299)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:214)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
at org.hibernate.engine.Cascades.cascade(Cascades.java:819)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:316)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:299)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:214)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:463)
at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:693)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:690)
at scheduler.dao.impl.HibEmployeeDAO.merge(HibEmployeeDAO.java:51)
at scheduler.employee.EmployeeController.saveemployee(EmployeeController.java:250)
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:585)
at org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:393)
at org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:331)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:348)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:264)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:216)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:195)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:148)
at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:90)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:595)
The hibernate mapping for the Employee object is :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//En"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="scheduler.Employee" table="employee">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="name" column="name" not-null="true"/>
<property name="dateCreated" not-null="true"/>
<property name="payRate"/>
<property name="payScale"/>
<property name="minHours" column="min_hours"/>
<property name="maxHours" column="max_hours"/>
<property name="maxPay" column="max_pay"/>
<property name="minPay" column="min_pay"/>
<property name="email" column="email"/>
<many-to-one name="user" class="scheduler.User" column="user_id" unique="true"/>
<many-to-one name="corporation"
column="corporation_id"
class="scheduler.Corporation"/>
<set name="locations" cascade="save-update" table="employee_location" lazy="false">
<key column="employee_id"/>
<many-to-many class="scheduler.Location" column="location_id"/>
</set>
<set name="EPLinks" cascade="all" inverse="true">
<key column="employee_id"/>
<one-to-many class="scheduler.EPLink"/>
</set>
<set name="SELinks" cascade="all" inverse="true">
<key column="employee_id"/>
<one-to-many class="scheduler.SELink"/>
</set>
<set name="calender" inverse="true" cascade="all">
<key column="employee_id"/>
<one-to-many class="scheduler.Calender"/>
</set>
<set name="fixedCalender" inverse="true" cascade="all">
<key column="employee_id"/>
<one-to-many class="scheduler.employee.fixedcalender.FixedCalender"/>
</set>
</class>
</hibernate-mapping>
The method where this error occurs is :
Code:
public ModelAndView saveemployee(HttpServletRequest request, HttpServletResponse response) throws Exception{
// Get Location
String error = null;
Location location = null;
boolean newEmployee = false;
try{
location = getLocationDAO().get(Integer.parseInt(request.getParameter("location")));
// If we're editing an employee.
int id = Integer.parseInt(request.getParameter("id"));
logger.debug("Searching for employee with id " +id);
Employee employee = getEmployeeDAO().get(id);
if(employee != null)
logger.debug("Found employee and his name is " +employee.getName());
// If we're making a new employee.
if(employee == null){
logger.debug("Making a new employee.");
employee = new Employee();
newEmployee = true;
}
// Set properties
employee.setName(request.getParameter("name"));
employee.getLocations().add(location);
employee.setCorporation(location.getCorporation());
employee.setEmail(request.getParameter("email"));
if(!newEmployee){
employee.setMaxHours(Integer.parseInt(request.getParameter("maxhours")));
employee.setMinHours(Integer.parseInt(request.getParameter("minhours")));
}
logger.debug("Employee's properties have been modified.");
if(request.getParameter("payrate") != null){
employee.setPayRate(Integer.parseInt(request.getParameter("payrate")));
} else {
employee.setPayRate(0);
}
if(request.getParameter("payscale") != null){
employee.setPayScale(Integer.parseInt(request.getParameter("payscale")));
} else {
employee.setPayScale(0);
}
// Clear the EPLinks out
logger.debug("Checking out the epLinks of the employee");
for (Iterator ep = employee.getEPLinks().iterator(); ep.hasNext();) {
EPLink elem = (EPLink) ep.next();
logger.debug("Getting the next EPLink. It is " +elem);
elem.getPosition().getEPLinks().remove(elem);
getEPLinkDAO().delete(elem);
}
employee.getEPLinks().clear();
//getEmployeeDAO().save(employee);
// Make new EPLinks
logger.debug("About to test positions");
String[] positions = request.getParameterValues("position");
if(positions != null){
logger.debug("Positions is not null");
for (int p = 0; p < positions.length; p++) {
int pid = Integer.parseInt(positions[p]);
Position pos = getPositionDAO().get(pid);
logger.debug("Positions are being found.");
if(pos != null){
EPLink epl = new EPLink(pos, employee);
pos.getEPLinks().add(epl);
employee.getEPLinks().add(epl);
}
}
}
logger.debug("saving employees");
getEmployeeDAO().save(employee);
logger.debug("Done.");
// Return XML.
response.getWriter().println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
response.getWriter().println("<response>");
//response.getWriter().println(" <ident>" + request.getParameter("ident") + "</ident>");
response.getWriter().println(" <newid>" + employee.getId() + "</newid>");
if(error != null){
response.getWriter().println(" <error>" + error + "</error>");
}
response.getWriter().println("</response>");
return null;
} catch (Exception ex){
response.getWriter().println("error");
response.getWriter().flush();
logger.error("There was an unrecoverable error", ex);
throw ex;
}
}
After searching for an answer, I tried evicting the object before I would update it. That did not, so I tried clearing the session, which was still unsuccessful. Any ideas?
Thanks,
Travis