0 Replies Latest reply on Nov 19, 2001 11:51 AM by callumw

    Creating and removing entity beans

    callumw Newbie

      Hi there,

      We are having a problem when creating, deleting and then creating and deleting the same entity. It seems a silly thing to do but then users do have twitchy fingers.

      The problem occurs following this scenario:
      1. create a new entity by calling ejbCreate() with a given primary key using BMP (commit option C - or A, B). This succeeds and a record is inserted in the database.
      2. delete the new entity by calling ejbRemove() on the entity. The first time you do this, ejbLoad() is called by the container (following an ejbActivate()) prior to the actual ejbRemove() method executing. This results in the pk being placed into the context so it is available inside the ejbRemove() method and can be used to delete the entity. The record is removed from the database.
      3. create an entity with the same primary key as the last entity. The record is inserted into the database.
      4. delete the new entity. This time when the ejbRemove() is invoked the primary key is not available in the EntityContext and so the operation fails. This may be because no ejbLoad() was called before the ejbRemove().

      The workaround is to get the EJBObject from the context in the ejbRemove() method and then get the pk from that.

      Can anyone clarify if this is a bug? or have we done something funny?


      I have snipped the following extract from the EJB 1.1 spec

      9.1.5 The entity bean component contract

      9.1.5.1 Entity bean instance's view:

      public void ejbRemove();

      ...

      The instance can still obtain the identity of the entity object via the getPrimaryKey() or getEJBObject() method of the EntityContext interface. The container synchronizes the instance's state before it invokes the ejbRemove method. This means that the state of the instance variables at the beginning of the ejbRemove method is the same as it would be at the beginning of a business method.

      ...

      9.1.5.2 Container's view:

      public void ejbRemove();

      ...

      The container must ensure that the identity of the associated entity object is still available to the instance in the ejbRemove() method (i.e. the instance can invoke the getPrimaryKey() or getEJBObject() method on its EntityContext in the ejbRemove() method).

      ...