I have a many to many relationship between the WoundCareProduct and ProductClass. Each wound care item can be associated with many product classes. Everthing works if I only save one association. When I save the second I get this errors(posted below).
Hibernate version: 3
Mapping documents:
WoundCareProduct
Code:
<hibernate-mapping>
<class name="bus.WoundCareProduct" table="PRODUCT">
<id name="id" type="int" unsaved-value="null" >
<column name="ID" sql-type="int" not-null="true"/>
<generator class="sequence">
<param name="sequence">SEQPRODUCT</param>
</generator>
</id>
<property name="name">
<column name="NAME"/>
</property>
<property name="description">
<column name="DESCRIPTION"/>
</property>
<!-- Associations -->
<!-- bi-directional many-to-many association to Class -->
<set
name="productclass"
lazy="false"
cascade="save-update"
table="PRODUCTCLASSLINK"
inverse="true"
order-by="CLASSID">
<key>
<column name="PRODUCTID" />
</key>
<many-to-many class="bus.ProductClass" >
<column name="CLASSID" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
ProductClass
Code:
<hibernate-mapping>
<class name="bus.ProductClass" table="CLASS">
<id name="id" type="int" unsaved-value="null" >
<column name="ID" sql-type="int" not-null="true"/>
<generator class="sequence">
<param name="sequence">SEQCLASS</param>
</generator>
</id>
<property name="name">
<column name="NAME"/>
</property>
<property name="description">
<column name="DESCRIPTION"/>
</property>
<!-- Associations -->
<!-- bi-directional many-to-many association to Product -->
<set
name="woundcareproduct"
lazy="false"
cascade="save-update"
inverse="false"
table="PRODUCTCLASSLINK">
<key>
<column name="CLASSID" />
</key>
<many-to-many class="bus.WoundCareProduct">
<column name="PRODUCTID" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():This is in a spring project so I will post everthing that I can.
Everything goes fine until the actual save is called. I can save one associated item to the table. If I try and save a second one I get the woundcareproduct with the two items associated from the product class, yet it throws the error below. there is no duplicate values in the database.
WoundCareProduct Class:
Code:
package bus;
import java.util.HashSet;
import java.util.Set;
public class WoundCareProduct {
private int id;
private String name;
private String description;
private Set productclass = new HashSet();
/// Getters
public String getDescription() {
return description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Set getProductclass() {
return productclass;
}
/// Setters
public void setDescription(String description) {
this.description = description;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setProductclass(Set productclass) {
this.productclass = productclass;
}
public void addProductclass(ProductClass productclass) {
// add wound care product to the productclass object
productclass.getWoundcareproduct().add(this);
// add the product class to the wound care product productclass set.
this.productclass.add(productclass);
}
}
Code:
public void updateWoundCareProduct(WoundCareProduct woundcareproduct) {
HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory);
hibernateTemplate.saveOrUpdate(woundcareproduct);
}
Product Class
Code:
package bus;
import java.util.HashSet;
import java.util.Set;
public class ProductClass {
int id;
String name;
String description;
private Set woundcareproduct = new HashSet();
/// Getters
public String getDescription() {
return description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Set getWoundcareproduct() {
return woundcareproduct;
}
/// Setters
public void setDescription(String description) {
this.description = description;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWoundcareproduct(Set woundcareproduct) {
this.woundcareproduct = woundcareproduct;
}
public int hashCode() {
int tmp = 0;
tmp = (id + name + description).hashCode();
return tmp;
}
}
Full stack trace of any exception that occurs:Code:
2005-09-06 10:51:00,641 ERROR [org.springframework.web.servlet.DispatcherServlet] - <Could not complete request>
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [bus.ProductClass#1]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [bus.ProductClass#1]
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [bus.ProductClass#1]
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.impl.SessionImpl.saveOrUpdate(SessionImpl.java:463)
at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:630)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:315)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:627)
at db.ProductManagerDaoHibernate.updateWoundCareProduct(ProductManagerDaoHibernate.java:53)
at bus.ProductManager.updateWoundCareProduct(ProductManager.java:57)
at web.EnterWoundCareProductController.onSubmit(EnterWoundCareProductController.java:39)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:361)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:335)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:245)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:355)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using: Oracle 9i
The generated SQL (show_sql=true): None generated.
Debug level Hibernate log excerpt: I am not sure how to turn this on when it is in a spring project.
Any help would be very appreciated.
Thanks