Until the next version of Jonas is out, here is some simple code to work 
around the fact that the current version doesn't null/default CMP fields 
prior to calling ejbCreate. Just call nullAllCMPFields() at the start of 
each ejbCreate. It uses the information stored in the bean descriptor to 
determine which fields to operate on.

All my entity beans derive from the same base class, so that's where I've 
put it. But it would be simple to make it a static function instead, if 
that works better for you. Obviously, once the next version of Jonas is 
out, you can get rid of it.

Joe

   /**Workaround for bug in Jonas 2.1.1: it doesn't set CMP fields to 
null/defaults
   when calling ejbCreate.
   This nulls out all CMP fields as declared by the bean descriptor.
   ===WARNING: THIS CODE IS HIGHLY DEPENDENT ON THE EJB SERVER (JONAS 
2.1.1)===
   However, this bug will be fixed in future Jonas versions, so you can get
   rid of this workaround then.*/
   protected void nullAllCMPFields() throws RemoteException
   {
        JEntityHome jeh = (JEntityHome)m_EntityContext.getEJBHome();
      EntityDesc desc = (EntityDesc)jeh.getDeploymentDescriptor();

      // Return if this bean doesn't use CMP.
      if (!(desc instanceof EntityCmpDesc)) return;

      EntityCmpDesc cmpDesc = (EntityCmpDesc)desc;

      // For all fields that occur in the CMP list, null them.
      Field[] fields = getClass().getFields();
      for (int iIndex = 0; iIndex < fields.length; iIndex++)
      {
        Field fld = fields[iIndex];
        if (cmpDesc.hasCmpFieldDesc(fld))
         {
                try
            {
                        nullCMPField(fld);
            }
            catch (Exception e)
            {
                throw new RemoteException("Exception while trying to null CMP 
fields",e);
            }
         }
      }
   }

   private void nullCMPField(Field fld) throws Exception
   {
        Class type = fld.getType();
      if (type.isPrimitive())
      {
         if (type.equals(Boolean.TYPE))
        fld.setBoolean(this,false);
         else if (type.equals(Byte.TYPE))
         {
                byte zero = 0;
                fld.setByte(this,zero);
         }
         else if (type.equals(Character.TYPE))
                fld.setChar(this,'\0');
         else if (type.equals(Double.TYPE))
                fld.setDouble(this,0);
         else if (type.equals(Float.TYPE))
        fld.setFloat(this,0);
        else if (type.equals(Integer.TYPE))
                fld.setInt(this,0);
         else if (type.equals(Long.TYPE))
                fld.setLong(this,0);
         else if (type.equals(Short.TYPE))
         {
                short zero = 0;
                fld.setShort(this,zero);
         }
      }
      else
        fld.set(this,null);
   }


=====================================================================
Joe Gittings, Royal Botanic Gardens, Kew
Hanover House, Kew, Richmond, Surrey TW9 3AB

[EMAIL PROTECTED]
+44 20 8332 5712
fax: +44 20 8332 5736

----
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body of the message "unsubscribe jonas-users".
For general help, send email to [EMAIL PROTECTED] and
include in the body of the message "help".

Reply via email to