for those experiencing this pain, here's some code that provides relief.
simply cut-paste the init() method into your bean, add the private class
variable myPublicFields, and add the init() call in your ejbCreate(). the
code handles reference types, modify to provide default values for non
reference types.
Initialization code - so a new EntityBean carries no values from the pool
-------------------------------------------------------------------------
private transient java.lang.reflect.Field[] myPublicFields = null;
private void init() {
try {
if (myPublicFields == null) {
java.lang.reflect.Field[] myFields =
this.getClass().getDeclaredFields();
java.util.Vector publicFields = new java.util.Vector();
for (int i=0; i<myFields.length; i++) {
if
(java.lang.reflect.Modifier.isPublic(myFields[i].getModifiers()))
publicFields.addElement(myFields[i]);
}
publicFields.copyInto(myPublicFields = new
java.lang.reflect.Field[publicFields.size()]);
myFields = null;
publicFields = null;
}
for (int i=0; i<myPublicFields.length; i++) {
try {
myPublicFields[i].set(this, null);
}
catch (IllegalAccessException e1) {
System.out.println("EJBean.init() - can't init every attribute -
IllegalAccessException");
}
catch (IllegalArgumentException e2) {
System.out.println("EJBean.init() - can't init every attribute -
IllegalArgumentException");
}
catch (NullPointerException e3) {
System.out.println("EJBean.init() - can't init every attribute -
NullPointerException");
}
catch (Exception e4) {
System.out.println("EJBean.init() - can't init every attribute -
unknown Exception");
}
}
}
catch (Exception e) {
// throw an error here if you want ejbCreate to fail if init doesn't
succeed
e.printStackTrace();
}
}
// to use init, put it in ejbCreate as the first data manipulator
public void ejbCreate() throws RemoteException, CreateException {
try {
init();
}
catch (Exception e) {
throw new CreateException(e.getMessage());
}
}
> -----Original Message-----
> From: Sachin Aggarwal [SMTP:[EMAIL PROTECTED]]
> Sent: Wednesday, March 17, 1999 11:39 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Inital values of activated beans: (was RE: ejbCreate( )
> - initialize instance variables
>
> > The initial values set for fields in an Entity bean by the container
> with
> > regard to activation, should be the default values of the
> > instance: null for
> > objects, zero for number primitives, and false for boolean.
>
>
> Looks like this would be nice but is not required by the spec. As a result
> the bean developer is forced to initialize intance variables to null ,
> zero
> for number primitives, and false for boolean. I wish the spec had that as
> a
> requirement of the container.
>
> This becomes important for Container managed beans where you create a
> bean,
> call the settors and do a commit. You expect the data in the underlying
> datastore to be null for instance variables that were not set and not what
> you set for some previous bean.
>
<snip>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".