Announcement Announcement Module
Collapse
No announcement yet.
a different object with the same identifier value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • a different object with the same identifier value

    I am new to spring and hibernate and am having issues with a manay-to-many setup. I have it where it will save data for a single mapping. However if I have more than one item associated it gives me the following error. To use the correct terms I have a Product and a Product Class. A product can be associated with many product classes. I can assign one product class to a product and save fine. assign the second one and it gives me this error. I am really thinking it is something simple but I just can't get it.


    Code:
    org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [bus.ProductClass#2]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [bus.ProductClass#2]
    	org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:656)
    	org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:413)
    	org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:320)
    	org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:627)
    	db.ProductManagerDaoHibernate.updateWoundCareProduct(ProductManagerDaoHibernate.java:53)
    	bus.ProductManager.updateWoundCareProduct(ProductManager.java:57)
    	web.EnterWoundCareProductController.onSubmit(EnterWoundCareProductController.java:40)
    	org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:361)
    	org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:335)
    	org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
    	org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:245)
    	org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
    	org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:355)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

    My Wound Care Product mapping

    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">
            <key>
                <column name="PRODUCTID" />
            </key>
            <many-to-many class="bus.ProductClass" >
                <column name="CLASSID" />
            </many-to-many>
        </set>
    
    </class>
    </hibernate-mapping>

    My product class mapping

    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>
    Here is the code that is in the controller to call the save: The form is a couple of text boxes and some check boxes.

    Code:
    public ModelAndView onSubmit&#40;Object woundcareproductbackingbean&#41; throws ServletException &#123;
    		
    		WoundCareProduct wcp = new WoundCareProduct&#40;&#41;;
    		wcp.setName&#40;&#40;&#40;WoundCareProductBackingBean&#41;woundcareproductbackingbean&#41;.getName&#40;&#41;&#41;;
    		wcp.setDescription&#40;&#40;&#40;WoundCareProductBackingBean&#41;woundcareproductbackingbean&#41;.getDescription&#40;&#41;&#41;;
    		productmanager.addWoundCareProduct&#40;wcp&#41;;
    		
    		Iterator it = &#40;&#40;WoundCareProductBackingBean&#41;woundcareproductbackingbean&#41;.getProductclass&#40;&#41;.iterator&#40;&#41;;
    		
    		while&#40;it.hasNext&#40;&#41;&#41;&#123;
    			int id = Integer.parseInt&#40;it.next&#40;&#41;.toString&#40;&#41;&#41;;
    			ProductClass pc = productmanager.getProductclass&#40;id&#41;;
    			wcp.addProductclass&#40;pc&#41;;
    		&#125;
    		productmanager.updateWoundCareProduct&#40;wcp&#41;;
    		
    		return new ModelAndView&#40;new RedirectView&#40;getSuccessView&#40;&#41;&#41;&#41;;
    	&#125;
    Here is the DaoHibernate code:


    Code:
    public void updateWoundCareProduct&#40;WoundCareProduct woundcareproduct&#41; &#123;
    		HibernateTemplate hibernateTemplate = new HibernateTemplate&#40;this.sessionFactory&#41;;
    		hibernateTemplate.saveOrUpdate&#40;woundcareproduct&#41;;
    	&#125;

    I have attached the source code to spring and hibernate and go to the point of knowing that it is something to do with the entity key generated but I just could not figure out what.

    I really need some help if anyone can. It would be greatly appreciated.

    Thanks

  • #2
    This looks like pure Hibernate problem to me.

    Anyways... Can you please post your productmanager.addWoundCareProduct(wcp) and wcp.addProductclass(pc)?

    Comment


    • #3
      I think you should try to user HibernateSession.merge() not HibernateSession.saveOrUpdate().

      Comment


      • #4
        I think you should try to user HibernateSession.merge() not HibernateSession.saveOrUpdate().
        That OR save in a different session OR make sure you load the object only once.

        Comment


        • #5
          Thanks for the replies. dejanp here is the code that you requested:

          addWoundCareProduct:

          Code:
          public void addWoundCareProduct&#40;WoundCareProduct woundcareproduct&#41; &#123;
          		HibernateTemplate hibernateTemplate = new HibernateTemplate&#40;this.sessionFactory&#41;;
          		hibernateTemplate.save&#40;woundcareproduct&#41;;
          		hibernateTemplate.flush&#40;&#41;;
          	&#125;

          Here is the two pojo's that are my classes. I forgot to post them:

          WoundCareProduct:


          Code:
          package bus;
          
          import java.util.HashSet;
          import java.util.Set;
          
          public class WoundCareProduct &#123;
          	private int id;
          	private String name;
          	private String description;
          	private Set productclass = new HashSet&#40;&#41;;
          	
          
          	///   Getters
          	
          	public String getDescription&#40;&#41; &#123;
          		return description;
          	&#125;
          	public int getId&#40;&#41; &#123;
          		return id;
          	&#125;
          	public String getName&#40;&#41; &#123;
          		return name;
          	&#125;
          	public Set getProductclass&#40;&#41; &#123;
          		return productclass;
          	&#125;
          	
          	
          	///   Setters
          	
          	public void setDescription&#40;String description&#41; &#123;
          		this.description = description;
          	&#125;
          	public void setId&#40;int id&#41; &#123;
          		this.id = id;
          	&#125;
          	public void setName&#40;String name&#41; &#123;
          		this.name = name;
          	&#125;
          	public void setProductclass&#40;Set productclass&#41; &#123;
          		this.productclass = productclass;
          	&#125;
          	
          	
          	public void addProductclass&#40;ProductClass productclass&#41; &#123;
          		// add wound care product to the productclass object
          		productclass.getWoundcareproduct&#40;&#41;.add&#40;this&#41;;
          		// add the product class to the wound care product productclass set.
          		this.productclass.add&#40;productclass&#41;;
          	&#125;
          &#125;

          ProductClass

          Code:
          package bus;
          
          import java.util.HashSet;
          import java.util.Set;
          
          public class ProductClass &#123;
          	int id;
          	String name;
          	String description;
          	private Set woundcareproduct = new HashSet&#40;&#41;;
          	
          	
          	///   Getters
          	
          	public String getDescription&#40;&#41; &#123;
          		return description;
          	&#125;
          	public int getId&#40;&#41; &#123;
          		return id;
          	&#125;
          	public String getName&#40;&#41; &#123;
          		return name;
          	&#125;
          	public Set getWoundcareproduct&#40;&#41; &#123;
          		return woundcareproduct;
          	&#125;
          	
          	
          	///   Setters
          	
          	public void setDescription&#40;String description&#41; &#123;
          		this.description = description;
          	&#125;
          	public void setId&#40;int id&#41; &#123;
          		this.id = id;
          	&#125;
          	public void setName&#40;String name&#41; &#123;
          		this.name = name;
          	&#125;
          	public void setWoundcareproduct&#40;Set woundcareproduct&#41; &#123;
          		this.woundcareproduct = woundcareproduct;
          	&#125;
          &#125;

          I ran across some info in a book by Wrox "Professional Hibernate" by Eric Pugh, and Joseph Gradecki that stated that I must implament the hashcode() method when using a set. Is this correct. Thanks so much for the help with this. If it is Hibernate I am very sorry and will put my post somewhere else, I don't want to waist anyones time.

          Thanks [/quote]

          Comment


          • #6
            Originally posted by croco
            I think you should try to user HibernateSession.merge() not HibernateSession.saveOrUpdate().
            Can you explain the difference between them. I tried the merge but didn't get any results. I thought that it would not insert the row for each association. If you could explain the difference that would be great.

            Thanks

            Comment


            • #7
              I have found two solution to this issue. Here it is, there are two.

              1. Use cascase = none.

              2. Use merge instead of saveandupdate. (actually thanks to croco who actually suggested this and I didn't get good results, maybe I did something else wrong but now it works with my current setup. Thanks croco)

              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 for all the help.

              Comment


              • #8
                My Discovery

                I got the Exactly the same Error when I was doing a Updation of a one-to-one mapping. I though it was because of I used bi-directional object setting. I used "merge".

                Anyway what had happen was the object that caused for the issue was with null ID when I read it. So... that Update gave this issue. I used web Service as my BL Layer with Transformers. After setting correct properties to each other to get the Object ID back in read and then Update solve my Problem.

                Success!

                - Uditha

                Comment

                Working...
                X