-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with HashCodeBuilder and EqualsBuilder
PostPosted: Sun Apr 22, 2007 11:32 am 
Newbie

Joined: Sun Apr 22, 2007 7:22 am
Posts: 3
Problem with HashCodeBuilder and EqualsBuilder

Hello
I am using JDK 6.0, Hibernate 3.2, MySQL 5.0.37, MySQL JConnector 5.0.5 and Apache commons lang 2.3

I have a class "Event" implemented hashCode and equals method:

Code:
package events;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class Event
{
   ...................

   @Override
   public int hashCode()
   {
      return HashCodeBuilder.reflectionHashCode( this );
   }

   @Override
   public boolean equals( Object obj )
   {
      return EqualsBuilder.reflectionEquals(this, obj);
   }

}

and I test in the main method :
package test;

import java.util.Date;
import java.util.Iterator;

import org.hibernate.Session;

import events.*;
import util.HibernateUtil;

public class Test
{   
   public static void main( String [] args )
   {
      Session session =    HibernateUtil.getSessionFactory().openSession();
       session.beginTransaction();
     
       //     ********* Test for insert
       Event event1 = new Event();
       event1.setDate(new Date());
       event1.setTitle("Title for event 1");
//       session.persist(event1);
     
       Person person1 = new Person();
       person1.setAge(25);
       person1.setFirstName("Duc");
       person1.setLastName("Nguyen");
//       person1.setEvent(event1);
     
       Person person2 = new Person();
       person2.setAge(25);
       person2.setFirstName("Long");
       person2.setLastName("Pham");
//       person2.setEvent(event1);

       event1.getParticipants().add(person1);
       event1.getParticipants().add(person2);
       session.save(event1);
//       session.save(person1);
//       session.save(person2);
     
     
       session.getTransaction().commit();
     
//       session.getSessionFactory().close();
     
       //********* Test for update
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       session.getTransaction().begin();     
//       testUpdate(session, 1L);
       Event anEvent = (Event) session.load(Event.class, new Long(1));
     
     
       System.out.println("\n===> anEvent hashCode: " + anEvent.hashCode() + "\tevent1 hashCode: " + event1.hashCode());
     
       if (anEvent.equals( event1 ))
          System.out.println("\n====> OK ");
       else
          System.out.println("\n====> fail");
   }
}   


The event id was saved into database with id "1"

This is the result in console:
===> anEvent hashCode: -1444564182 event1 hashCode: -1444574986

====> fail

What problem happend to Apache commons lang library ? or I implement equals, hashCode incorrectly ?

Please help me. Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 22, 2007 12:02 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:44 pm
Posts: 33
Hi!

Can the problem here be the relationship from event to participants. Does reflectionHashCode include Collections?

You might want to try to comment out the following lines
Code:
event1.getParticipants().add(person1);
event1.getParticipants().add(person2);


and see if the hashcodes still are different.


-Kaj :)


Top
 Profile  
 
 Post subject: Problem with class containing fields as colllection ?
PostPosted: Sun Apr 22, 2007 1:10 pm 
Newbie

Joined: Sun Apr 22, 2007 7:22 am
Posts: 3
Thanks kajh
I comment out the following lines
Code:
event1.getParticipants().add(person1);
event1.getParticipants().add(person2);

but the test still failing
When I test with the Person class (does not contain a field as a collection) I get the result as expected
Why ? How to solve the problem with class containing fields as collection ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 22, 2007 2:20 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:44 pm
Posts: 33
Hi!

I see that there is a version of which offer you to exclude some fields, see

reflectionEquals(Object lhs, Object rhs, String[] excludeFields)

(from http://tinyurl.com/2ffxhr)

Try to exclude the fields that are Collections (and refefrences to other domain objects) and let me know how that works out.


-Kaj :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 10:19 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:44 pm
Posts: 33
kajh wrote:
Hi!
Try to exclude the fields that are Collections (and refefrences to other domain objects) and let me know how that works out.


Just curious: have you solved this issue?


-Kaj :)


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