-->
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: set returns different number of elements
PostPosted: Thu Aug 23, 2007 10:18 am 
Newbie

Joined: Thu Aug 23, 2007 9:22 am
Posts: 2
Hi all,

we currently have a some problems using one-to-many and many-to-many associations. We're developing a simple web application which does
nothing more than to retrieve an object from the backend and prints out the associated objects in the jsp.

Unfortunately this returned list contains a different number of elements, if reloading the page. We currently have no additional caching and this
happens whether the fetch type is eager or lazy, but we have this problems in multiple places throughout the application. Currently, the only
way to get around this problem is directly using SQL to retrieve the objects. Is there any better way of doing this or anything we might have missed?

Sorry, if this problem has already been discussed before, but I couldn't find anything about it.

Thanks in advance, Joerg


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 12:35 pm 
Newbie

Joined: Fri Mar 30, 2007 5:23 pm
Posts: 16
Location: New York, NY
Problems with collections are often related to equals() and hashCode() implementations. Can you post your equals() and hashCode() method(s) for the object(s) that are being collected in the Set?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 3:51 am 
Newbie

Joined: Thu Aug 23, 2007 9:22 am
Posts: 2
matbrown wrote:
Problems with collections are often related to equals() and hashCode() implementations. Can you post your equals() and hashCode() method(s) for the object(s) that are being collected in the Set?


The hashCode and equals methods of the objects we put into the set are taking into account all attributes except the ID (the database id). For example:

The class MultipleImageUpload:

Attributes:

Code:
        @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
   
   private Long ticket;
   
   @Lob
   private URI location;
   
   //and more attributes and associations which are not used in the equals and hashCode methods


The hashCode is computed as:

Code:
   @Override
   public int hashCode()
   {
      final int PRIME = 31;
      int result = 1;
      result = PRIME * result + ((location == null) ? 0 : location.hashCode());
      result = PRIME * result + ((ticket == null) ? 0 : ticket.hashCode());
      return result;
   }


The equals method looks like:

Code:
        @Override
   public boolean equals(Object obj)
   {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final MultipleImageUpload other = (MultipleImageUpload) obj;
      if (location == null)
      {
         if (other.location != null)
            return false;
      }
      else if (!location.equals(other.location))
         return false;
      if (ticket == null)
      {
         if (other.ticket != null)
            return false;
      }
      else if (!ticket.equals(other.ticket))
         return false;
      return true;
   }


The location and ticket attributes are actually never null, we are sure of that. Also, the database entries for ticket and location are different for each entry. So no two MultipleImageUpload objects should have the same hashCode and be equal.

Do we have to add the id to the hashCode and equals computations? I would understand that we have to add the id to those computations if the objects were added to the set before their (other) attributes are set. Is that so?

Thanks in advance for any pointers.
--
Joerg


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.