View RSS Feed

My Java Tips

EJB - Entity Beans

Rate this Entry
by , 11-26-2011 at 07:39 PM (1395 Views)
This post contains an introduction to Entity Beans.


An entity beans are used to represent a business object in a persistent storage mechanism for example: customers, orders, products, employees etc. We normally refer persistent storage mechanism as a relational database. Normally, each entity bean has an underlying table in a relational database. Please note that each instance of the bean corresponds to a row in that table.

Some people mix Entity beans with session means. Both are different in many ways. Some properties of Entity beans are listed below which are not present in Session beans:

- persistent
- shared access
- primary keys
- may participate in relationships with other entity beans

Lets talk about persistence. The state of Entity bean is saved in a storage mechanism, therefore it is persistent. It means that the entity beanís state exists beyond the lifetime of the application or the J2EE server process. You all know that the data in a database is persistent because it stays even after database server or the applications are turned off/shutdown.



Multiple clients can share Entity beans. This is useful because there may exist situations where multiple clients might want to change the same data. So it is important that entity beans work within transactions. EJB container (JBOSS, BEA Web Logic, Web Sphere) provides transaction management, so you as a developer should not be bothered about this. For this, you have to specify the transaction attributes in the bean's deployment descriptor.

Now lets talk about primary key in Entity bean. It is important that each entity bean has a unique object identifier. For example, an employee entity bean, will be identified by a unique employee number. This primary key, enables the client to locate a particular entity bean.




A primary key class must meet the following requirements:

- class must be serializable
- access control modifier of the class must be public.
- all fields must be declared as public.
- fields must be a subset of the bean's persistent fields.
- public default constructor is a must
- class should implement the hashCode() and equals(Object other) methods

Lets take an example. The example class has a composite primary key.

Java Code:
public class PurchaseOrderKey implements java.io.Serializable {

  public String productModel;
  public String vendorId;

  public PurchaseOrderKey() { };

  public boolean equals(Object other) {

    if (other instanceof PurchaseOrderKey) {
      return (productModel.equals(
          ((PurchaseOrderKey)other).productModel) &&
          vendorId.equals(
          ((PurchaseOrderKey)other).vendorId));
    }
    return false;
  }

  public int hashCode() {
    return productModel.concat(vendorId).hashCode();
  }
}

Submit "EJB - Entity Beans" to Facebook Submit "EJB - Entity Beans" to Digg Submit "EJB - Entity Beans" to del.icio.us Submit "EJB - Entity Beans" to StumbleUpon Submit "EJB - Entity Beans" to Google

Categories
Java EE

Comments