// TestPK.java
class AccountPK implements java.io.Serializable
{
private int _hashCode = Integer.MIN_VALUE;
public java.lang.String id;
public AccountPK(java.lang.String id)
{
this.id = id;
}
public int hashCode()
{
if (_hashCode == Integer.MIN_VALUE)
{
_hashCode += this.id.hashCode();
}
return _hashCode;
}
public boolean equals(Object obj)
{
AccountPK pk = (AccountPK)obj;
boolean eq = true;
eq = eq && this.id.equals(pk.id);
return eq;
}
}
public class TestPK
{
public static void main (String args[])
{
AccountPK key1 = new AccountPK("key1");
AccountPK key2 = new AccountPK("key1");
CacheKey key3 = new CacheKey(key1);
CacheKey key4 = new CacheKey(key2);
if( key3.hashCode() != key4.hashCode() )
System.out.println("key3, key4 hashCode test failed");
if( key3.equals(key4) == false )
System.out.println("key3, key4 equals test failed");
AccountPK key11 = new AccountPK("key1");
AccountPK key21 = new AccountPK("key1");
key21.hashCode(); // Cause key21._hashCode value to update
CacheKey key31 = new CacheKey(key11);
CacheKey key41 = new CacheKey(key21);
if( key31.hashCode() != key41.hashCode() )
System.out.println("key31, key41 hashCode test failed");
if( key31.equals(key41) == false )
System.out.println("key31, key41 equals test failed");
}
}
Produces:
key31, key41 hashCode test failed
key31, key41 equals test failed
Why? Because Serialization includes the private state of the object. This is
a perfectly valid PK which is not handled correctly by CacheKey. If the
_hashCode value is marked as transient all works. The assumption being
made by CacheKey is that the only ivars in the PK are its fields that define
its identity.
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development