Creating a new ejbCreate method

When the enterprise bean is created, the ejbCreate method is automatically generated. This method is then promoted to the remote interface, so that it is available in the access bean. The default ejbCreate method only contains parameters that are either the primary key or part of the primary key. This means that only those values are instantiated upon bean instantiation. If your enterprise bean contains fields that are not part of the primary key and are non-nullable fields, you must create a new ejbCreate method in which you specifically instantiate those fields. By doing so, each time a new record is created, all non-nullable fields will be populated with the appropriate data.

Before you begin

You have added the finders to your bean as required.

About this task

To create a new ejbCreate method:

Procedure

  1. In the Project Explorer view, double-click the YourNewBeanBean class to open it and view its source code. The initial ejbCreate method might not initialize all non-nullable fields. In the UserRes example, where the user ID is the primary key, the initial ejbCreate method does not initialize the rooms and home fields:
    
    public UserResKey ejbCreate(int argUserId)
       throws javax.ejb.CreateException 
    {
       _initLinks();        
       userId = argUserId;
       return null;
    }
    
  2. Add a new ejbCreate method so that it accepts an initial value for every non-nullable CMP field and initializes these fields accordingly. Continuing the UserRes example, add the following method which takes all non-nullable fields as parameters and sets their values:
    
    public UserResKey ejbCreate(int argUserId, String argHome, byte
    Rooms)
       throws javax.ejb.CreateException 
    {
       _initLinks();
    
       // All CMP fields should be initialized here.        
       userId = argUserId;
       home = argHome;
       rooms = argRooms;
    
       return null;
    }
    
    Note: If you want to use a system generated primary key, refer to Primary keys for details.
  3. You must add the new ejbCreate method to the home interface. This makes the method available in the generated access bean. To add the method to the home interface:
    1. Right-click the ejbCreate( yourParameters) method in the Outline view and select Enterprise Bean > Promote to Home Interface.
  4. Proceed to initialize optimistic locking fields in ejbCreate