I've not been able to keep up with my blogging schedule because too many things are going on at the same time. I built some EJB 3.0 examples lately and wanted to share how EJB 3.0 is going to simplify our life. Entity beans have become very simple with EJB 3.0. I still remember the day when I successfully built an EJB 1.1 entity bean. If you have hated EJB 2.1 entity beans due to its complexity and have been a fan of O-R Mapping framework like TopLink then you will really love EJB 3.0. Annotations make things cleaner without having to do the complex mapping in an XML file. Here is very simple entity bean that I developed and deployed to our EJB 3.0 container that we are building. This is based on the classic EMP table in the SCOTT schema of an Oracle database.
@Table(name = "EMP")
public class Employee implements java.io.Serializable
private int empNo;
private String eName;
private double sal;
public int getEmpNo()
public void setEmpNo(int empNo)
this.empNo = empNo;
public String getEname()
public void setEname(String eName)
this.eName = eName;
public double getSal()
public void setSal(double sal)
this.sal = sal;
If you look at the entity bean this is a POJO and it does not require any interfaces or deployment descriptors. I annotated with @Entity to mark this as an Entity bean. I used @Table annotation to specify the table name and marked @Id to mark EMP_NO as the primary key for the table.
I created a Session Bean façade and used the EntityManager API to create, query, update, etc entity bean instances.
public class EmployeeFacadeBean implements EmployeeFacade
private EntityManager em;
private Employee emp;
public void addEmployee(int empNo, String eName, double sal)
if (emp == null) emp = new Employee();
If you look at the EntityManager API Iíve used its pretty straight forward. You do not have to do any complex lookup. You just use dependency injection to inject EntityManager instance and invoke create method to create an entity bean instance.
EJB 3.0 is really going to simplify developers' life.