-->
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.  [ 3 posts ] 
Author Message
 Post subject: NonUniqueObjectException -- what am I missing?
PostPosted: Thu Oct 09, 2008 5:51 pm 
Newbie

Joined: Thu Oct 09, 2008 5:15 pm
Posts: 6
I'm receiving a NonUniqueObjectException and I'm not sure how I can avoid it. I know what the exception means, and I pretty much know why I'm getting it, but I'm hoping I'm just missing an easy way to fix it. Here's my scenario:

I have a simple Message class:
Code:
@Entity
public class Message {

   private int id;
   private String value;

   @Id
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getValue() {
      return value;
   }

   public void setValue(String value) {
      this.value = value;
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + id;
      result = prime * result + ((value == null) ? 0 : value.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      ProjectEntityMessage other = (ProjectEntityMessage) obj;
      if (id != other.id)
         return false;
      if (value == null) {
         if (other.value != null)
            return false;
      } else if (!value.equals(other.value))
         return false;
      return true;
   }
}


Which is being used inside of a Spring Hibernate DAO kind of like this:

Code:
Message m1 = new Message(1, "abc");
Message m2 = new Message(2, "def");
Message m3 = new Message(1, "abc");

// parent has a List<Messages> attribute
Parent p1 = new Parent();
parent.getMessages().add(m1);
parent.getMessages().add(m2);

Parent p2 = new Parent();
parent.getMessages().add(m2);
parent.getMessages().add(m3);


// has a List<Parent> attribute
ParentOfParent eldest = new ParentOfParent();
eldest.getParents().add(p1);
eldest.getParents().add(p2);

getHibernateTemplate().save(eldest);


The logs show all of the insert statements for inserting p1 go off ok, but as soon as p2 tries to handle the m3 message, I get the NonUniqueObjectException. This example is a simplification, but my use case requires that I may have to create multiple objects corresponding to a single database row. I know that m1 != m3, but m1.equals(m3) should be true and I thought Hibernate used that to determine identity.

What am I missing?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2008 2:01 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Inside a single Hibernate session object identity is required (and guaranteed). What this means is that if obj1.equals(obj2) is true then obj1 == obj2 must also be true. See http://www.hibernate.org/hib_docs/v3/re ... s-identity

One possible way to fix this can be to use factory with caching capabilities to create your Message object. Eg.

Code:
MessageFactory factory = new MessageFactory();
Message m1 = factory.get(1, "abc");
Message m2 = factory.get(2, "def");
Message m3 = factory.get(1, "abc");


The factory should contain logic to make sure that m3 == m1.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2008 10:25 am 
Newbie

Joined: Thu Oct 09, 2008 5:15 pm
Posts: 6
Hmm ok that explains it. I thought you just had to guarantee that obj1.equals(obj2) was true. Under the circumstances, I think your suggestion of the object factory caching message objects is my best solution. Thanks!


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