-->
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.  [ 6 posts ] 
Author Message
 Post subject: NonUniqueObjectException after refresh and then update
PostPosted: Wed Feb 03, 2010 7:30 pm 
Newbie

Joined: Wed Feb 03, 2010 7:06 pm
Posts: 4
I promise I searched the heck outta this forum, google, and all of cyberspace before coming here to ask you guys what the heck is going on. My next step after this forum post is to pull my hair right out of my head.

my version: 3.2.1.ga , but i have also tested in 3.3.2.GA, and the EXACT same result is happening there too, so I'm sure it's something I am doing wrong.

Alrighty, here is my goal:

1. User decides to cancel what they've done so far. They click cancel
2. I will open a new session and then...
3. I will refresh() the object they are working on so it is populated with whatever is in DB
4. I will close session
5. User will work on the object again, and make changes to the data in some of the attributes
6. User will click save
7. I will open a new session and then...
8. I will update() the object so that their recent changes go into DB.
BANG RIGHT HERE I GET NonUniqueObjectException.
9. Finally { close session }

Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [us.mn.state.dot.puma.model.RelationshipDTO#1243]

Obviously 1243 is the primary key of the object I'm trying to refresh and then update in this example, but it will happen with any primary key....

Some background:
This is a fully functioning application, the cancel feature is something I am just adding in now. The user can open up an existing object and make changes and save/update it all day long without any NonUniqueObjectException. It is only after a refresh that i cannot save/update!!

My model:

RelationshipDTO has many-to-one relationship to an ElementDTO object
Code:
<hibernate-mapping package="us.mn.state.dot.puma.model">
     <class name="RelationshipDTO"  table="BUNDLE_RELATIONSHIP" lazy="false">
       <id name="relationshipId" column="BUNDLE_RELATIONSHIP_ID">
          <generator class="sequence">
             <param name="sequence">BUNDLE_RELATIONSHIP_ID</param>
         </generator>
        </id>

        <property name="primeRelationship" column="PRIME_ELEMENT_FLAG" type="yes_no" />

        <many-to-one name="bundle" class="BundleDTO" column="BUNDLE_ID" cascade="save-update,persist"/>
        <many-to-one name="element" class="ElementDTO" column="ELEMENT_ID" cascade="all" />
        <many-to-one name="primeElement" class="ElementDTO" column="PRIME_ELEMENT_ID" cascade="save-update,persist"/>
        
    </class>
   
   </hibernate-mapping>


and my ElementDTO:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   <hibernate-mapping package="us.mn.state.dot.puma.model">
     <class name="ElementDTO"  table="ELEMENT" lazy="false">
       <id name="elementId" type="java.lang.Long" column="ELEMENT_ID">
          <generator class="assigned" />
        </id>
        
        <property name="spNumber" column="CORE_SP_NBR" type="string" />
        <property name="spExt" column="SP_EXT" type="string" />
        <property name="description" column="ELEMENT_DESC" />
        
   

      <set name="relationships" lazy="false" table="BUNDLE_RELATIONSHIP"
         cascade="all" inverse="true">
          <key column="ELEMENT_ID"/>
          <one-to-many class="RelationshipDTO" />
       </set>
       
    </class>
   
    <sql-query name="element.nextId">select ELEMENT_ID_SEQ.nextval from dual</sql-query>
   
   </hibernate-mapping>



FURTHERMORE:

I thought the problem would have to do with the fact that I "detached" the object after refreshing by closing the session. Nope! Just for testing purposes, I attempted to refresh and then update all within the same session - same problem. Here's my code:

Code:
         session.refresh(dto);
         
         Transaction tx = session.beginTransaction();
         session.update(dto);
         tx.commit();
         
         session.close();


any help? losing my marbles here


Last edited by mndot_lance on Wed Feb 03, 2010 7:42 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: NonUniqueObjectException after refresh and then update
PostPosted: Wed Feb 03, 2010 7:40 pm 
Newbie

Joined: Wed Feb 03, 2010 7:06 pm
Posts: 4
Also:

In an attempt to fix this solution, I did try overriding the equals and hashcode methods, which was advised by some of the blogs and forum posts i found on this topic. Here's the overrides:

Code:
@Override
   public boolean equals(Object object)
   {
       if (this == object)
       {
           return true;
       }
       if (!(object instanceof RelationshipDTO))
       {
           return false;
       }
       final RelationshipDTO that = (RelationshipDTO)object;
       if (this.relationshipId == null || that.getRelationshipId() == null || !this.relationshipId.equals(that.getRelationshipId()))
       {
           return false;
       }
       return true;
   }

   @Override
   public int hashCode()
   {
       int hashCode = 0;
       hashCode = 29 * hashCode + (relationshipId == null ? 0 : relationshipId.hashCode());

       return hashCode;
   }


Top
 Profile  
 
 Post subject: Re: NonUniqueObjectException after refresh and then update
PostPosted: Sat Feb 06, 2010 10:32 am 
Newbie

Joined: Mon Feb 01, 2010 1:04 pm
Posts: 6
edit: Upps, sorry I mistook your code for JPA, hibernate API uses update instead of merge

I'm a newbie to hibernate/JPA as well, but wouldn't a modified detached object better be added to a session with EntityManager.merge()? as step 7.5 in your procedure


Top
 Profile  
 
 Post subject: Re: NonUniqueObjectException after refresh and then update
PostPosted: Mon Feb 08, 2010 1:13 pm 
Newbie

Joined: Wed Feb 03, 2010 7:06 pm
Posts: 4
Nope, I'm not using JPA, so I don't think EntityManager.merge() is really an option for me. Thank you for the thought, though.

Any other ideas out there? Still completely stumped by this.


Top
 Profile  
 
 Post subject: Re: NonUniqueObjectException after refresh and then update
PostPosted: Mon Feb 08, 2010 7:40 pm 
Newbie

Joined: Wed Feb 03, 2010 7:06 pm
Posts: 4
Well, I lost a couple days of development on this simple problem, but here's the solution for someone down the line who has this problem:

public void refresh(DTO dto) throws HibernateException, SQLException {
HibernateSession.currentSession().load(dto, dto.getId() );
}

instead of

public void refresh(DTO dto) throws HibernateException, SQLException {
HibernateSession.currentSession().refresh(dto);
}

refresh() should be avoided. load() will load data from db and refresh the
persistent object with same id in session, too.


Top
 Profile  
 
 Post subject: Re: NonUniqueObjectException after refresh and then update
PostPosted: Mon Apr 26, 2010 8:23 am 
Beginner
Beginner

Joined: Sat Sep 04, 2004 7:07 am
Posts: 20
Location: Helsinki, Finland
To use session.load(...) instead of session.refresh(...) solved for me the problem of a 'NonUniqueObjectException' when I tried to update after refresh.

Surprisingly the NUOException appeared only in POJOs but not when I ran precisely the same code in application server.

_________________
Risto


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