-->
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.  [ 4 posts ] 
Author Message
 Post subject: Just a simple example but got NonUniqueObjectExcept
PostPosted: Thu Aug 27, 2009 10:43 am 
Newbie

Joined: Thu Aug 27, 2009 10:35 am
Posts: 6
Code:
// persistence context started

         User newUser = new User(userName, Encoder.encodePassword(password));
         this.session.save(newUser);
         Type newTypeIn = new Type("new type", newUser, 'I');
         this.session.save(newTypeIn);
         Type newTypeOut = new Type("new type", newUser, 'O');
         this.session.save(newTypeOut);

// transaction commit and persistence context end


Then got a Exception:org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [entities.Type#0]

I read the book <<Java Persistence With Hibernate>>, It said, this Exception is because the detached Object was reattached in Session but there was already another Object with the same identifier in the Session. The solution is use session.merge() instead of session.update(). But that's not my Situation, my problem is about new created Object, so what should I do?

Thanks!


Top
 Profile  
 
 Post subject: Re: Just a simple example but got NonUniqueObjectExcept
PostPosted: Thu Aug 27, 2009 12:25 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
What is the primary key for NewType?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Just a simple example but got NonUniqueObjectExcept
PostPosted: Thu Aug 27, 2009 4:00 pm 
Newbie

Joined: Thu Aug 27, 2009 10:35 am
Posts: 6
Cameron McKenzie wrote:
What is the primary key for NewType?


int id, it's an auto generated field in database

Code:
public class Type {


   @Id
   private int id;
   private String name;
   @JoinColumn(name="username", referencedColumnName="name")
   private User user;
   private char type;  // 'I'ncome or 'O'utput
   
   public Type() {
      
   }
   
   public Type(String name, User user, char type) {
      this.setName(name);
      this.setUser(user);
      this.setType(type);
   }
......


Top
 Profile  
 
 Post subject: Re: Just a simple example but got NonUniqueObjectExcept
PostPosted: Thu Aug 27, 2009 6:21 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, it looks like your save isn't giving these entities new identifiers.

Quote:
[entities.Type#0]


That tells me both entities have an identifier of zero. The save method returns the primary key. Maybe you can log it and see if you're getting a non-zero number.

Have you overridden the .equals method? That might help?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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