-->
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.  [ 7 posts ] 
Author Message
 Post subject: Many-to-many saveandupdate
PostPosted: Tue Sep 06, 2005 11:12 am 
Newbie

Joined: Tue Mar 29, 2005 5:34 pm
Posts: 19
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

_________________
Robert Fulcher


Top
 Profile  
 
 Post subject: I solved this problem although I don't know the reason
PostPosted: Tue Sep 06, 2005 3:24 pm 
Newbie

Joined: Tue Sep 06, 2005 3:22 pm
Posts: 2
I had the same situation as yours (Oracle 9). Please check you hibernate config file. Remove them if you see the following configs,

<property name="hibernate.connection.isolation">8</property>

<property name="hibernate.jdbc.batch_size">0</property>


Top
 Profile  
 
 Post subject: Re: I solved this problem although I don't know the reason
PostPosted: Tue Sep 06, 2005 3:49 pm 
Newbie

Joined: Tue Mar 29, 2005 5:34 pm
Posts: 19
charlieliujcn wrote:
I had the same situation as yours (Oracle 9). Please check you hibernate config file. Remove them if you see the following configs,

<property name="hibernate.connection.isolation">8</property>

<property name="hibernate.jdbc.batch_size">0</property>


Thanks for the info, however; I am using the hibernate sessions setting in my application context.xml. I don't have those two mappings. I have posted my file for reference.

Thanks

Code:

<beans>
   <!-- Main Controller Class -->
   <bean id="WoundCareProductsController" class="web.WoundCareProductsController" >
      <property name="productmanager">
         <ref bean="ProductManager"/>
      </property>
   </bean>
   
   <!-- Validator for Case Form -->
       <bean id="WoundCareProductValidator" class="web.WoundCareProductValidator" />
      
       <bean id="EnterWoundCareProductController" class="web.EnterWoundCareProductController">
          <property name="sessionForm"><value>true</value></property>
          <property name="commandName"><value>woundcareproduct</value></property>
          <property name="commandClass"><value>bus.WoundCareProductBackingBean</value></property>
          <property name="validator"><ref bean="WoundCareProductValidator"/></property>
          <property name="formView"><value>woundcareproduct</value></property>
          <property name="successView"><value>main.htm</value></property>
          <property name="productmanager">
            <ref bean="ProductManager"/>
         </property>
       </bean>
   
   <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="mappings">
         <props>
            <prop key="/main.htm">WoundCareProductsController</prop>
            <prop key="/woundcareproduct.htm">EnterWoundCareProductController</prop>
         </props>
      </property>
   </bean>

   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename"><value>messages</value></property>
    </bean>
   
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
        <property name="prefix"><value>/WEB-INF/jsp/</value></property>
        <property name="suffix"><value>.jsp</value></property>
    </bean>
   
    <!-- This will set the ProductManager with the correct DAO access -->
       <bean id="ProductManager" class="bus.ProductManager">
          <property name="productManagerDao">
             <ref bean="ProductManagerDao"/>
          </property>
       </bean>
      
    <!-- This will set the sessionFactory of the ProductManagerDaoHibernate object -->
       <bean id="ProductManagerDao" class="db.ProductManagerDaoHibernate">
           <property name="sessionFactory">
             <ref bean="mySessionFactory"/>
          </property>
       </bean>
       
    <!-- Hibernate SessionFactory -->
      <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref bean="dataSource"/></property>
        <property name="mappingResources">
          <list>
            <value>woundcareproduct.hbm.xml</value>
            <value>productclass.hbm.xml</value>
          </list>
        </property>
        <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
          </props>
        </property>
      </bean>
   
   <!-- JNDI dataSource -->
       <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName">
            <value>java:comp/env/jdbc/WoundCareProducts</value>
         </property>
      </bean>
</beans>


_________________
Robert Fulcher


Top
 Profile  
 
 Post subject: Help
PostPosted: Thu Sep 08, 2005 8:50 pm 
Newbie

Joined: Tue Mar 29, 2005 5:34 pm
Posts: 19
Is there any other thing that I can do to get this to work. I am willing to change my whole configuration, I just need to make this work. Should I use a list instead of a set???? Someone has to be using this.

Any help please......


Thanks

_________________
Robert Fulcher


Top
 Profile  
 
 Post subject: Solution
PostPosted: Fri Sep 16, 2005 7:09 pm 
Newbie

Joined: Tue Mar 29, 2005 5:34 pm
Posts: 19
I have found my own solution in this issue. Here it is, there are two.

1. Use cascase = none.

2. Use merge instead of saveandupdate.

I got it to work and hope this will help other people who are out there and run into this issue. I am not sure which one is the right solution but they both work. This is directly opposite to what is listed in "the bible of Hibernate" on pages 227 to 230. Which all examples have cascade = save-update.

Thanks

_________________
Robert Fulcher


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 10:15 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
You are using Hibernate 3. The bible is based on H2.
In addition. the merge(..) approach is preferred.

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 8:11 am 
Newbie

Joined: Tue Mar 29, 2005 5:34 pm
Posts: 19
Jason Zhicheng Li wrote:
You are using Hibernate 3. The bible is based on H2.
In addition. the merge(..) approach is preferred.


Jason,

You are right in both cases. I will use the merge function. Thanks for the info and thanks to everyone for there comments and help and for this great product.

Thanks

_________________
Robert Fulcher


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.