How to manage fields that can have null values in a container-managed Entity bean?

Shaun Childers

First of all, let's just set up a typical scenario:

You are developing a product which allows a bank to sign up new customers online. You have done analysis and design and settled on having two tables: 'users' and 'account' (let's keep it simple). Each "user" will have a corresponding "account". The foreign key between the two will be the "account number".

So, for the 'users' table, you have the following fields: firstName, lastName, userId, password, and acctNum. When this table is created in the database, it is empty. Now you must relate your EJB code to this table for persistence. For simplicity sake I will leave out the Session bean (which I would use to talk to my Entity bean), the Entity bean primary key class, and the home and remote interfaces.

We have the UserBean:

public class UserBean implements javax.ejb.EntityBean {
  public String firstName = null;
  public String lastName  = null;
  public String userId    = null;
  public String password  = null;
  public long   acctNum   = -1;

/**
 * Called by the container after the UserHome.create() is called
 */
  public void ejbCreate(String userId, String password, long acctNum) {
  this.userId   = userId;
  this.password = password;
  this.acctNum  = acctNum;
  }
...
...
  public void setUserData(UserData data) throws RemoteExeption, UserDataException {
    this.firstName = data.getFirstName();
    this.lastName  = data.getLastName();
  }
...
...
}

Now, assuming you have the User (remote interface class), UserHome (home interface class), UserPK (primary key class) already done, you need to create the bean's deployment descriptor. Inside the deployment descriptor, you must specify the database table, 'users', which this bean will map to. Also, you must specify which fields from your bean map to which fields in the 'users' database table. (This is how the container knows which fields to persist between your bean and the database table.) Now assuming all code compiles and you have an EJB server up and running and you have deployed your bean, all you need to do is write a client (I would use a client to access a session bean, say 'CustomerSession', which would talk to my entity bean) to create a new user and pass in the userId, password and acctNum values, which will create the new user in the database.

Notice the fields in the UserBean will now be set, but the firstName and lastName fields will still be set to null. These fields will still be empty in the database and will not change until these fields are set in the bean, since it is persisted. Now, call the setUserData(UserData data) method for setting the firstName and lastName, and these fields will now be set in the database and no longer be null. The container will handle the persistence of any fields which are set to null, just as it will handle any fields which are set to some meaningful value.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact