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
About this task
To create a new ejbCreate method:
Procedure
- 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
andhome
fields:public UserResKey ejbCreate(int argUserId) throws javax.ejb.CreateException { _initLinks(); userId = argUserId; return null; }
- 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. - 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:
- Right-click the ejbCreate( yourParameters) method in the Outline view and select Enterprise Bean > Promote to Home Interface.
- Proceed to initialize optimistic locking fields in ejbCreate