Java Tutorial - Java Scipt : Creating the User Entity Bean

Java Tutorial - Java Scipt :

Creating the User Entity Bean


Entity beans are persistent Enterprise Java Beans. It is easiest to think of an entity bean as corresponding to a specific row in a database table or result set. Entity bean persistence can be managed either by the EJB container (CMP) or by the programmer (BMP). We will implement a CMP bean for the User domain object. The CMP Bean class must be an abstract class that implements the javax.ejb.EntityBean interface. The EJB container generates a concrete class that extends this class when the bean is deployed. The following code shows this class:
package com.oldfriends.ejb;
import com.oldfriends.dto.UserDTO;
import com.oldfriends.utils.KeyMaker;
import java.lang.*;
/** Entity bean representing an application user
* @author John T. Bell
*/
public abstract class UserEjb implements javax.ejb.EntityBean
{
public UserEjb(){}
public String ejbCreate(
String firstName,
String lastName,
String mi,
String maidenName,
int gradYear,
String loginId,
String email,
String password
)
throws javax.ejb.CreateException
{
setUserid();//Dynamically generates a new key value
setFirstName( firstName );
setLastName( lastName );
setMi(mi);
setMaidenName( maidenName);
setGradYear( gradYear );
setLoginId( loginId );
setPassword( password );
setEmail( email);
return null;
}
public String ejbCreate( UserDTO userBean )
throws javax.ejb.CreateException
{
setUserid();//Dynamically generates a new key value
setFirstName( userBean.getFirstName());
setLastName( userBean.getLastName() );
setMi(userBean.getMi());
setMaidenName( userBean.getMaidenName());
setGradYear( userBean.getGradYear());
setLoginId( userBean.getLoginId() );
setPassword( userBean.getPassword() );
setEmail( userBean.getEmail() );
return null;
}
public void ejbPostCreate(
String firstName,
String lastName,
String mi,
String maidenName,
int gradYear,
String loginId,
String email,
String password
)
{}
private void setUserid()
{
this.setUserid(KeyMaker.getNewKey());
}
public void ejbPostCreate( UserDTO userBean ){}
public abstract String getUserid();
public abstract void setUserid( String uid );
public abstract String getFirstName() ;
public abstract void setFirstName( String firstName ) ;
public abstract String getLastName() ;
public abstract void setLastName( String lastName ) ;
public abstract String getMi() ;
public abstract void setMi( String mi ) ;
public abstract String getMaidenName() ;
public abstract void setMaidenName( String maidenName ) ;
public abstract int getGradYear() ;
public abstract void setGradYear( int gradYear ) ;
public abstract String getLoginId() ;
public abstract void setLoginId( String loginId ) ;
public abstract String getPassword() ;
public abstract void setPassword( String password ) ;
public abstract String getEmail() ;
public abstract void setEmail( String email ) ;
public void ejbLoad(){}
public void ejbStore(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setEntityContext(javax.ejb.EntityContext ctx){}
public void unsetEntityContext(){}
public void ejbRemove() throws javax.ejb.RemoveException{}
}

Notice that the only code is in the ejbCreate methods and the private setUserid() method. The setUserid() method is called by the ejb Create methods to generate a new GUID when adding a record to the database. Neither ejbCreate method accepts a value for the userid column of the database. Instead, it calls the setUserid() method, which in turn calls a utility class that generates a new value for the primary key. The following code shows this utility class:
package com.oldfriends.utils;

import org.jboss.util.id.GUID;
public class KeyMaker
{
void KeyMaker(){}
public static String getNewKey()
{
GUID guid = new GUID();
return guid.toString();
}
}
We want this in a separate class because we have used a function that is specific to JBoss to create the key.

 When you want to move this code to another container besides JBoss, you will want to replace this class with one that is not tied to this JBoss function. We are using the JBoss function just because it is convenient to do so when running in this JBoss environment.