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: HashCode - Equals
PostPosted: Tue Jan 11, 2005 6:00 pm 
Newbie

Joined: Wed Oct 06, 2004 4:39 pm
Posts: 17
Hi,

I have the next situation:

A---------B-----------C
1........N N........1 <---- association

class A {
private long id; <----- Primary Key
private String keyA; <----- Business key
private Set b;
....
}

class B {
private long id; <----- Primary Key
private A a;
private C c;
....
}
mapping

class C {
private long id; <----- Primary Key
private String keyC; <----- Business key
private Set b;
....
}

Mapping

Class A
<class
lazy="true"
....
<set
name="b"
lazy="true"
<one-to-many
class="B"
.....
>

Class B
<many-to-one
name="a"
outer-join="auto"
...
>
<many-to-one
name="b"
outer-join="auto"
...
>

Class C
<class
lazy="true"
....
<set
name="b"
lazy="true"
<one-to-many
class="B"
.....
>


Is it ok the next code in B class?

public boolean equals(Object other) {
if (!(other instanceof B)) {
return false;
}
B castOther = (B) other;
return new EqualsBuilder().
append(this.getA().getKeyA(),castOther.getA().getKeyA()).
append(this.getC().getKeyC(),castOther.getC().getKeyC()).
isEquals();
}

public int hashCode() {
return new HashCodeBuilder().
append(this.getA().getKeyA()).
append(this.getC().getKeyC()).
toHashCode();
}

My doubt is that when I retrieve B via HQL or Criteria, Hibernate execute aditional sql select under A and C. Why? Because the code in equals?

Thanks.

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 7:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes, key() is lazy loaded and need a DB access to be filled.
BTW you should do appendgetA(), other.getA());
And delegate the comparison to the equals implem of A, it's more elegent and less intrusive

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 08, 2007 4:57 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 4:26 am
Posts: 26
Location: Germany, Dortmund
Hi!

Even it is an old posting, I think it is still up to date. I have read the very good description about hashCode() and equals() in 'Java Persistence with Hibernate' and understand why to use the business key.

However I see two problems with it:

As described above the usage of the business key can make from an lazy association (very useful!) an non-lazy one which means additional DB calls.

The second problem is in a high concurrent system, an access like A.getB().size() can leads into an ObjectNotFoundException. Why? For filling the Set of Bs, the Set uses B.hashCode(). If this hashCode() depends on an other association with B1 and B1 is part of the business key of B, B1 must be loaded for B.hashCode(). If between the select for the Bs and the select for B1 the B instance with the associated B1 was deleted, throws A.getB().size() an an ObjectNotFoundException. An ObjectNotFoundException during access to a set ist not very intuitive.
There is a possibility to avoid this by using the primary key instead of the business key from B1 in B.hashCode(). But this means that you can only add persistent B1 to B which makes POJO unit tests impossible :-(

Bye
Kai


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.