EJB - Entity Beans

NO LONGER EXISTS --- DEPRECATED and REPLACED by Java Persistence API

>>>>>>>there have an "Entity Object" notion to replace. See Java Persistence API
>>>>>>>see our class webpage under outline for Java Persistence API information

Characteristics:

  • Are persistent objects
    • typically associated with a database entry
    • data and bean are synchronized
  • Can be container-managed (CMB) or bean-managed (BMB)
  • Multiple clients can share entity beans
  • Use transactions to alter data from client.
  • Entity bean has concept of primary key as in relational databases.
  • Entity beans can be related to each other like tables in a database can be related
    • OrderEJB and ProductEJB and CutomerEJB

Entity Bean Methods

javax.ejb.EntityBean interface

 

setEntityContext

  • use to set associated entity context
  • context is access to runtime entity context ....identify caller, access current transaction state, change state, obtain primary key.

unsetEntityContext

  • Container calls before removing instance.

ejbActivate

  • same as for session beans

 

ejbPassivate

  • same as for session beans

 

ejbLoad

  • container invokes this method to instruct the instance to synchronize its state by loading it from underlying database.
  • BMP: instance variables are refreshed by writting code here to read from the database.
  • CMP: container loads the bean's state from the database.

 

ejbStore

  • container invokes this method to instruct the instance to synchronize its state by storing it to the underlying database.
  • BMP: write code to update database
  • CMP: prepare container-managed fields to be writtent to database.

ejbRemove

  • same as for Session bean

ejbCreate

  • Container calls after create EJB

ejbPostCreate

  • Container calls after ejbCreate()....can use to further initialize bean

findbyPrimaryKey(String key)

  • allows client to locate entity beans.
  • BMP: define in the home interface and implement a corresponding method that begines with prefix ejbFindByPrimaryKey
  • CMP: you DO NOT write this method. GENERATED automatically by the container
  • e.g. of use:

    StudentLocalHome home = .....;
    StudentLocal student = home.findByPrimaryKey("123445555");

findbyX(Object x)

  • allows client to locate entity beans.
  • BMP: define in the home interface and implement a corresponding method that begines with prefix ejbFindByPrimaryKey
  • CMP: you DO NOT write this method. GENERATED automatically by the container
  • e.g. findbyLastName(String lastname)

 

Bean-Managed

public class StudentEJB implements EntityBean {

private String studentID; //also primary key
private String firstName;
private String lastName;
private String address;

public String ejbCreate(String s, String f, String l, String a) throws CreateException {

studentID =s;
firstName =f;
lastName = l;
address = a;

 

Conection con = null
PreparedStatement ps = nu;;

try {

con = getConnection();
ps = con.preparedStatement("insert into students (student_id, "+ " first_name, lastname, address) values (?,?,?,?)");

ps.setString(1, studentID);
ps.setString(1,firstName);
ps.setString(1, lastName);
ps.SetString(1,addresss);

if(ps.executeUpdate() != 1) {

String error = "JDBC did not create any row";
log(error);
thow new CreateException(error);
}

 

return studentID;
} catch(SQLException sqe) { ....} finally { cleanup(con,ps); }

}

}

 

public interface StudentLocalHome extends EJBLocalHome {

StudentLocal create(String studetnID, String name, String password, String address) throws CreateException;

......
}

client creates a new entity object

StudentLocalHome home =

Student student = home.create("1,", "Sam", "password", "123 first ave, Hayward, CA");

 

 

 

Container-Managed Persistence

 

no JDBC code in the ejbCreate method!!!

You must specify the following in the deployment descriptor so that the container can do the management:

  • persistence fields
  • relationship fields
  • schema
  • queries

public abstract class OrderEJB implements EntityBean {

//get and set methods for cmp fields
public abstract String getOrderID();
public abstract void setOrderID(String id);
public abstract java.sql.Timestamp getOrderDate();
public abstract void setOrderDate(java.sql.Timestamp timestamp);
public abstract String getStatus();
public abstract void setStatus(String status);

public abstract double getAmount();
public abstract void setAmount(double amount);

//get and set methods for relationship fileds
public abstract Collection getLineItems();
public abstract void setLineItems(Collection lineItems);

public abstract StudentLocal getStudent();
public abstract void setStudent(StudentLocal student);

public Stgring ejbCreate(String orderID, StudentLocal student, String status, double amount) throws CreateException {

setOrderID(orderID);
set OrderDate(new java.sql.TImestamp(System.currentTimeMillis()));
setStatus(status);
setAmount(amount);

return null;

}

}

 

persistent fields

represent and store single unit of data

typically mapped to columns of a table the entity bean is mapped to

container synchronizes the state with the database entry state.

e.g. orderDate, status, amount

How to access: through get* and set* methods in the EJB.

relationahip fields

represents and stores a reference to another entity bean

like "foreign key".

e.g. lineItems, student

Abstract Persistent Schema

located in deployment descriptor

abstract representation of an entity bean's persistent fields and relationship fields.

<cmp-version>2.x</cmp-version>
<abstract-schemap-name>Order</abstract-schema-name>
<cmp-field> <field-name>orderID</field-name> </cmp-field>

<cmp-field> <field-name>orderDate</field-name></cmp-field>

<cmp-field> <field-name> status </field-name></cmp-field>

<cmp-field> <field-name>amount</field-name></cmp-field>

.....

version = 2.x if you want EJB 2.0 container managed persistence

declares 4 container-managed fields

names MUST match the get* and set* methods

EJB Query Language

  • new in EJB 2.0
  • similar to SQL
  • lets you write queries in methods in database independent way.

 

 

© Lynne Grewe