The primary key's generated hashCode() method stores
its value in a transient int _hashCode.  It only
generates the value if the _hashCode ==
Integer.MIN_VALUE.  

If the object is serialized the _hashCode is not saved
because it is transient, and when deserialized
_hashCode is initialized to ZERO not MIN_VALUE.  This
causes the hashCode() method to never recalculate the
value of _hashCode() after being serialized.

Attached is the generated primary key class with a
main method demonstrating this problem.

Also attached is the bean I used with Xdoclet to
generate this primary key class. 

__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/
package ejbtest;

import javax.ejb.*;

/**
 * @ejb:bean
 *    name="EntityA"
 *    jndi-name="test/EntityA"
 *    local-jndi-name="test/EntityA"
 *    type="CMP"
 *    cmp-version="2.x"
 *    view-type="both"
 *    unchecked="true"
 *    schema="EntityA"
 *
 * @ejb:transaction type="Required"
 *
 */
public abstract class EntityABean implements javax.ejb.EntityBean {

	/**
     * Create EntityA.
     * @ejb:create-method
     */
	public EntityAPK ejbCreate(int id)
        throws CreateException {

        setId(id);

        return null;
	}


	public void ejbPostCreate(int id) throws CreateException {}

	/**
	 * Primary Key setter, don't call after the create method.
	 * @param newValue The new value of the Id
	 */
	 public abstract void setId(int newValue);

	/**
	 * @return Id
	 * @ejb:interface-method view-type="both"
	 * @ejb:persistent-field
	 * @ejb:pk-field
	 */
	public abstract int getId();

	public void setEntityContext(javax.ejb.EntityContext entityContext) {}

	public void unsetEntityContext() {}

	public void ejbLoad() {}

	public void ejbActivate() {}

	public void ejbPassivate() {}

	public void ejbRemove() {}

	public void ejbStore() {}
}
/*
 * Generated file - Do not edit!
 */
package ejbtest;

import java.lang.*;
//import javax.ejb.*;
import java.rmi.MarshalledObject;

/**
 * Primary key for EntityA.
 * @xdoclet-generated at Mar 18, 2002 11:30:40 AM
 */
public class EntityAPK
   extends java.lang.Object
   implements java.io.Serializable
{
   static final long serialVersionUID = -8757892072328236367L;
   transient private int _hashCode = Integer.MIN_VALUE;
   transient private String value = null;

   public int id;

   public EntityAPK()
   {
   }

   public EntityAPK( int id )
   {
      this.id = id;
   }

   public int getId()
   {
      return id;
   }

   public void setId(int id)
   {
      this.id = id;
   }

   public int hashCode()
   {
      if( _hashCode == Integer.MIN_VALUE )
      {
         _hashCode += (int)this.id;
      }

      return _hashCode;
   }

   public boolean equals(Object obj)
   {
      if( !(obj instanceof ejbtest.EntityAPK) )
         return false;

      ejbtest.EntityAPK pk = (ejbtest.EntityAPK)obj;
      boolean eq = true;

      if( obj == null )
      {
         eq = false;
      }
      else
      {
         eq = eq && this.id == pk.id;
      }

      return eq;
   }

   public String toString()
   {
      if( value == null )
      {
         value = "[.";
         value += this.id+".";
         value += "]";
      }

      return value;
   }

    public static void main(String[] args) throws Exception 
    {
	EntityAPK pk = new EntityAPK(55);
	System.out.println("pk.hashCode(): "+ pk.hashCode());

	MarshalledObject mo = new MarshalledObject(pk);
	System.out.println("marshalled.pk.hashCode(): "+((EntityAPK)(mo.get())).hashCode());
    }
}

Reply via email to