Inside Scoop on J2EE : Tips and tricks on J2EE and Oracle Application Server by Debu Panda
Updated: 3/2/2005; 8:28:12 AM.

 

Subscribe to "Inside Scoop on J2EE" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

Thursday, February 17, 2005

 

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.

 

@Entity

@Table(name = "EMP")

public class Employee implements java.io.Serializable

{

   private int empNo;

   private String eName;

 

   private double sal;

   

 

   @Id

   @Column(name="EMPNO", primaryKey=true)

 

   public int getEmpNo()

   {

      return empNo;

   }

 

   public void setEmpNo(int empNo)

   {

      this.empNo = empNo;

   }

 

   public String getEname()

   {

      return eName;

   }

 

   public void setEname(String eName)

   {

      this.eName = eName;

   }

 

 

   public double getSal()

   {

      return sal;

   }

 

   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.

 

@Stateless

public class EmployeeFacadeBean implements EmployeeFacade

 

{

   @Inject

   private EntityManager em;

   private Employee emp;

 

   public void addEmployee(int empNo, String eName, double sal)

   {

      if (emp == null) emp = new Employee();

      emp.setEmpNo(empNo);

      emp.setEname(eName);

      emp.setSal(sal);

      em.create(emp);

   }

..

}

 

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.

 

 Bye!


4:43:38 AM    comment []

© Copyright 2005 Debu Panda.

PS: These are my own thoughts and not of my employer ..



Click here to visit the Radio UserLand website.
 


February 2005
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28          
Jan   Mar