import java.util.Random;
public class A
{
// the object id, unsaved=0;
private long id=0;
private String value;
public boolean equals(Object obj)
{
if (obj==this)
{
/*
* This actually does not wotk simetime.
* In some case I know two objects refer the same
instance,
* however the control flow does not return from
here.
*
* I do not know why, may be caused by the proxy
implementation.
*/
return true;
}
if (obj instanceof A)
{
A a=(A)obj;
// judge the equality of persistence objects based
on id
/*
* Use getId() here. Do not use the id member
variable directly.
* The getId() sometime returns a value different
from that of id member variable.
*
* Is this because that the Proxy implementation
* intercept the getId() call?
*
*/
// at least one instance is a persistent
instance.
if ((getId()+a.getId())!=0)
{
// if the id is equals, they are equal.
if (getId()==a.getId())
return true;
else
return false;
} else
{
// judge the equality base on field semantic.
if (value.equals(a.getValue()))
return true;
return false;
}
} else
return false;
}
/**
* Id is a very good candidate for hashCode.
* It guaranttes that the id is different for every
instance for the same type of object.
*
* The rule is that the hashCode() can not change in
one Hibernate
* session even the information(value member
variable here)
* used in equals() comparison changes. Why? This is
a consideration for the java.util.HashSet
* implementation. Suppose you have an object in
java.util.Hashset, you modify the object value
* and the HashCode() right now returns a value
different from that before the object is inserted
* into the HashSet. Right now, you have no way to
remove the object from the HashSet.
* The Hashset assumes that the hashCode() returns an
invariable value.
*
* For this reason, if the transient object (id is 0)
is changed to persistent object, its hashCode()
* can not be changed according to the id value.
Therefore we cache the hashCode() here. Once the
hashCode()
* is called by the JVM, it is invariable.
*
*
* However this implementation violate the hashCode()
contract in some case.
* For example, suppose that an transient object is
persisted with a id(500) (hashCode is randomly
generated).
* From other source(session), we get another object
with a id (500) (hashCode is 500).
* These two objects are equal, but the hashCode()
is different.
*
* This kind of situation is ignored here.
Application developer should understand the impact of
* the hashCode() implementation and take action to
avoid such case.
*/
private int hashcode=-1;
public int hashCode()
{
if (hashcode==-1)
if (id==0)
hashcode=(new Random()).nextInt();
else
hashcode=(int)id;
return hashcode;
}
public long getId()
{
return id;
}
public void setId(long l)
{
id= l;
}
public String getValue()
{
return value;
}
public void setValue(String string)
{
value= string;
}
}
hope my experience is helpful.
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel