Inside Scoop on Java EE : Tips and tricks on enterprise java, middleware technologies and Oracle Application Server
Updated: 8/11/2006; 11:50:53 AM.

 

Home

Subscribe to "Inside Scoop on Java EE" 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.

 
 

Friday, August 11, 2006

Changes are part of our life! New car, new computer or new kids are always fun! They bring excitement to life.
But sometime we have emotional attachment to old things, old houses, etc.
And I had the same with this blog-site and was resisting for a change.

Finally I decided to desert it.

Please visit my new blog site: http://debupanda.com

RSS: http://debupanda.blogspot.com/rss.xml
11:50:51 AM    comment []

Wednesday, August 09, 2006

My laptop crashed about a month back and I'm still trying to recover. I've been very careless about backing up my stuffs and I had to price for carelessness. Lack of time in past one year is one of the primary drivers for my carelessness though. The last time I took backup of my laptop was about two months back and I lost many documents, articles, presentations, test cases, and samples that I wrote in last two months. A high stressed PM job and night book authoring does not mesh well (:  One good thing was that I used a different machine for book writing and backed up my work almost every day. Probably this incident will teach me a lesson to be little more careful next time.
 
The worst effect was on my blogs. I did not realize that if you use Radio Userland all blog entries are stored on your local drives and recovery is not easy and I was almost decommissioned from blogging for about a month. However the folks in Radio Userland helped me recover my old posts and hope I'm back in business again. I think I should seriously plan out a transition of blogs. I like Google's blogger. But now that it is blocked from India does not excite me. Got any suggestions for a good blog site that is reliable and that will not leave out of blogging for weeks?

1:54:28 PM    comment []

Tuesday, July 11, 2006


EJB 3.0 is now final and many projects have started using EJB 3.0. The good news is that Spring is building some EJB 3.0 support as part of Pitchfork project that will widen acceptance of EJB 3.0. There may be some projects you want to use both EJB 3.0 Session beans and Spring-managed beans.  For example you may have a Stateless EJB that you want to access in your POJO in your web module.

If you have used EJB 2.1 Session beans from Spring then you remember that you had to use a business interface and Springís proxy classes that helped avoid writing Service locator and repetitive JNDI code.  Unfortunately Spring documentation has not been updated how-to access EJB 3.0 Session beans from Spring. Hence this information in not from the source and result of my trial. I could make this to work and in this blog I will outline the steps that  to access EJB 3.0 Session beans from Spring Applications.

The Stateless EJB 

Let us assume that you have an EJB 3.0 Session bean named EmployeeFacade that is used to create and update employees as in the following code:

@Remote
public interface EmployeeFacade {
Employee addEmployee(String name, double salary);
Employee findEmployeeByEmpNo(Long empNo);
}

@Stateless
public class EmployeeFacadeBean  implements EmployeeFacade {
@PersistenceContext
   private EntityManager em;

      public Employee addEmployee(String empName, double sal) {
         Employee emp = new Employee();
         emp.setName(empName);
         emp.setSal(sal);
         em.persist(emp);
         return emp;
      }

  public Employee findEmployeeByEmpNo(Long empNo){
      return em.find(Employee.class,empNo);
}

}


Remember that EJB 3.0 Session beans are POJOs and interfaces are business interfaces and hence you do not have to add an extra business interface as use of EJB 2.1 with Spring required.

Spring POJOs

You want to use the EmployeeFacade Session bean in your Spring bean and you can use setter injection to inject an instance of the EJB 3.0 Session bean as follows:

public class EmployeeFacadeServiceBean implements EmployeeFacadeService  {

protected EmployeeFacade employeeFacade ;
     
  //setter Injection
  public void setEmployeeFacade(EmployeeFacade employeeFacade) {
     this.employeeFacade = employeeFacade;
  }


   public Employee addEmployee(String empName, Double sal){
       return (Employee) this.employeeFacade.addEmployee(empName, sal);  
   }

    public Employee findEmployeeByEmpNo(Long empNo) {
       return (Employee) this.employeeFacade.findEmployeeByEmpNo(empNo);
    }

}

This Spring bean is accessed from the web module.

Spring Configuration

Now let us see the Spring configuration that does the magic:

1. First EJB 3.0 does not require the proxy classes and related configure that you need for EJB 2.x. This is due to the fact that EJB 3.0 does not require a home interface. You can use the  JndiObjectFactoryBean to wire the EJB in Spring configuration as follows:

<bean id = "employeeFacade"                                  
  class = "org.springframework.jndi.JndiObjectFactoryBean">
    <property name = "jndiName"
              value = "java:comp/env/ejb/EmployeeFacade"/>
</bean>


Note that Iíve used a name in the ENC to make it portable. If you want to use a global JNDI name for your EJB, you can do that too. Also note that there is no difference in using either a remote or local interface for EJB 3.0 Session bean because the interfaces are POJIs and what differs is references defined for the interface used that we will see next.

2. You have to make sure that the EJB is referenced from the web module either using ejb-ref or ejb-local-ref in the web.xml or using @EJB annotation in the class level as follows:


@EJB(name="ejb/EmployeeFacade", beanInterface=oracle.ejb30.EmployeeFacade.class)
public class InsertServlet extends HttpServlet
{

..}


3. You configure the POJO using the EJB (EmployeeFacade) as follows:

<bean id="employeeFacadeService" class="oracle.ejb30.EmployeeFacadeServiceBean">
     <property name = "employeeFacade"  ref = "employeeFacade"/>
  </bean>

Is this simple enough? You can combine the power of Spring and EJB 3.0 without much complexity. In my next blog I will discuss how you can develop a Spring-enabled EJB 3.0 Session bean.



9:40:29 AM    comment []

Thursday, June 08, 2006


EJB3 specification was finalized few weeks back and now EJB3 and JPA are in action and many companies have developing their next generation applications using EJB3 and JPA.

 

OnJava published my article on EJB3 JPA few weeks back and I got some follow up questions from readers and customers.  Can you map an entire hierarchy of objects with multi-level inheritance into a single table?

 

In my article, I discussed a single level of inheritance and will use the same domain model to add another level.

 

Let us start with a simple top-level domain object Employee. There may be two type of employees: FullTime and Contractor and hence FullTime and Contractor inherit from Employee object. Let us assume that there are two types of contractors: Consultant who charges hefty amount and Volunteer who works for free and hence Consultant and Volunteer inherit from Contractor object and hence the inheritance will look like follows:

 

Let us assume that you want to map the entire hierarchy of objects into a single table named EMP table.

 

 

Assuming you do not to persist an instance of Employee as is, we defined it to be an abstract entity and defined that it is mapped to EMP table and we have defined the inheritance strategy to SINGLE_TABLE.

 

We have defined the discriminator column to be EMPLOYEE_TYPE that stores the employee type: F for fulltime, C for Consultant and V for volunteer. Note that Id and Table mapping can be defined only in the top-level entity in a hierarchy as follows when using SINGLE_TABLE inheritance strategy:

 

 

@Entity

@Table(name="EMP")

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)

@DiscriminatorColumn(name="EMPLOYEE_TYPE",discriminatorType=DiscriminatorType.STRING, length=1)

public abstract class Employee implements Serializable {

    @Id

    @GeneratedValue(strategy=GenerationType.AUTO)

    @Column(nullable=false)

    protected Long empNo;

    @Column(name="ENAME")

    protected String name;

    protected Timestamp hireDate;

    protected String job;

    ..

}

 

The FullTime entity extends the employee and can be persisted on its own. We define the DiscriminatorValue for this entity to be 'F'

 

@Entity

@DiscriminatorValue(value="F")

public class FullTime extends Employee {

public FullTime() {

    }

    @Column(name = "SAL")

    private Double salary;

    @Column(name = "COMM")

    private Double commission;

    @Column(name = "DESIG")

    private String designation;

..

}

 

The Contractor entity extends Employee but is not persistence on its own, instead its sub-type Consultant and Volunteer are persisted. Hence there is no discriminator value defined for the Contractor entity:

 

@Entity

public abstract class Contractor extends Employee  {

  @Column(name = "END_DATE")
   private Timestamp endDate; 

..

}

 

The discriminator value for Consultant and Volunteer entities are defined as follows:

 

@Entity

@DiscriminatorValue(value="C")

public class Consultant extends Contractor {

    public Consultant() {

..

}

           

 

 

@Entity

@DiscriminatorValue(value="V")

public class Volunteer extends Contractor implements Serializable {

    public Volunteer() {

    }

..

}

 

 

 

We are done with the mapping !

 

Now let us create a session façade for the Employee entity

 

@Stateless(name="EmployeeFacade")                                  

public class EmployeeFacadeBean implements EmployeeFacade {

    @PersistenceContext

    private EntityManager em;

 

    public EmployeeFacadeBean() {

    }

 

    public Object mergeEntity(Object entity) {

        return em.merge(entity);

    }

    public Object persistEntity(Object entity) throws EmployeeCreationException{

        em.persist(entity);

        return entity;

    }

 

..

  }

 

Let us create a client that uses the session facade persists each type of Employee instances and then list using a named query. You can use dependency injection to invoke the bean instance as follows: 

 

            @EJB

            EmployeeFacade employeeFacade;

 

      // Persist a FullTime Entity

            FullTime fte = new FullTime();

            fte.setName("PNisheet");

            fte.setSalary(new Double(1200.0));

            fte.setDesignation("Programmer");

          

            employeeFacade.persistEntity(fte);

         

            // Persist a Consultant

 

            Consultant pte = new Consultant();

            pte.setName("PandaB");

            pte.setHourlyRate(new Double(100.0));

            employeeFacade.persistEntity(pte);

           

           //Persist a Volunteer

            Volunteer vol = new Volunteer();

            vol.setName("RenuMi");

            vol.setDesignation("Trainee");

            employeeFacade.persistEntity(vol);

           

            // List All employees

            List<Employee> emplist = employeeFacade.findAllEmployee();

 

We are done. After running the the client you will see each entity instance persisted to the EMP table with appropriate EMPLOYEE_TYPE column set. 

 

EMPNO ENAME      EMPLOYEE_TYPE
----- ---------- -------------
 1506 RenuMi     V
 1504 PNisheet   F
 1505 PandaB     C

 

Hope this helps!



10:28:06 AM    comment []

Tuesday, May 30, 2006


I took a break from work last week and went for a weeklong cruise trip to Alaska with my family. It was a nice break from work and relaxing. Now I feel as if I got my battery recharged!  Alaska is beatiful too. I canít believe I did not browse web or read emails (cell phone switched off, isn't that nice?) for six days! Now Iíve hundreds of emails in my Inbox. I tried the high-speed Internet on the vessel from the cruise on my final day on cruise and it was worse than a dialup. It was expensive, $16 for half-hour and all I could do is write only three e-mails!

Anyway back to work and will soon resume regular tech blogging!



6:22:54 AM    comment []

Thursday, May 18, 2006


EJB3 became final few days back. OnJava just published my article Standardizing Java Persistence API with EJB3 JPA. This article provides an introduction to what EJB3 JPA is bringing onto the table.



11:51:16 AM    comment []

Monday, May 15, 2006


"In this age of hyper-competitiveness, learning a new technology by balancing a book on your lap while hacking away at the business problem on the keyboard has become the norm. But letís face itósomewhere deep down, you probably prefer "baptism by fire."

 

This Chapter is for the brave pioneer in all of us, who can take a quick look at all the EJB types  .."

 

The early draft of Chapter 2: First Taste of EJBs of our upcoming book EJB3 In Action   is now available at Manning Website. This introduces different types of EJBs: Session beans, MDBs and Entity.

 

Please review and give us your honest feedback.



5:04:42 AM    comment []

© Copyright 2006 Debu Panda.



Click here to visit the Radio UserLand website.
 


August 2006
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 29 30 31    
Jul   Sep