Wednesday, June 27, 2007

JDJ Article: Java EE 5 and Spring

JDJ just published second part of my article on Java EE 5/ EJB 3 integration with Spring. You can read the article in the June 2007 issue. Here is link to the article for your reading pleasure.

Thursday, June 21, 2007

EJB 3 In Action is #1 spot of hot new released Java Books


EJB 3 In Action got the #1 spot of best selling new and future release Java books. Note that Amazon keeps on changing the rank every hour


Here is the screen shot that shows that it's #1

Wednesday, June 06, 2007

IndicThreads Java Coding Contest and EJB 3 In Action

India's premier Java Developer portal IndicThreads is holding a Java coding contest. This week winner gets a copy of my book EJB 3 In Action!

Also EJB 3 In Action has been reprinted in India by DreamTech press and is available now at most computer book stores!

Monday, June 04, 2007

Injecting POJOs and using resource injection in POJOs

Java EE 5 greatly simplifies using resources and EJBs by using dependency injection. However this support limits only to managed classes such as EJBs, Interceptors, Servlets, etc. Many applications use helper classes those are regular POJOs. However Java EE 5 / EJB 3 does not require support of dependency injection with regular POJOs or injecting POJOs into managed classes.


In this article, I will discuss about Oracle’s extension of managed POJOs and describes how to use dependency injection such as @Resource or @PersistenceContext with regular POJOs (in OC4J 11 Preview).


This example has a session bean named EmployeeFacade that injects a regular POJO named EmployeeHelper. The EmployeeHelper POJO uses @PersistenceContext to inject an instance of EntityManager.


Using Annotations in regular POJOs


To use dependency injection with regular POJO it does not require any Oracle specific extension. The only requirement is that it must be injected to another managed class by using @oracle.j2ee.annotation.InjectedObject or using Oracle specific descriptor e.g. orion-ejb-jar.xml.



Following is an example of regular POJO that uses dependency injection.


public class EmployeeHelper {
@PersistenceContext
private EntityManager em;
private Employee emp;
public Employee findById(int empNo) {
return ((Employee) em.find(Employee.class, empNo));
}
public Employee saveEmployee(String eName, double sal) {
..
return emp;
}
}

Injecting a POJO
OC4J 11 allows injecting POJOs to a managed class or to another POJO using @oracle.j2ee.annotation.InjectedObject. To inject the EmployeeHelper class into the EmployeeFacade EJB we need the following code:


@Stateless
public class EmployeeFacadeBean implements EmployeeFacade {
@InjectedObject
private EmployeeHelper eh;

public Employee findEmployeeByEmpNo(int empNo) {
return eh.findById(empNo);
}
public int addEmployee(String eName, double sal) {
return eh.saveEmployee(eName,sal).getEmpNo();
}
}


Conclusion

OC4J 11 allows injecting one POJO to another POJO. You can try this with OC4J 11 Preview and let us know what other features you want to see.

Another Five STAR rating for EJB 3 In Action

Another reviewer of JavaLobby (Michael Smolyak ) gave five star rating to EJB 3 In Action. Here is what he says:

"The book is enjoyable, extremely well organized and covers a wide range of EJB subjects. I would recommend EJB 3 in Action to both old hands and newly-initiated in the EJB craft. My only complaint is the size of the book; but I think the logical, unhurried and deliberate manner, with which the authors approached the non-trivial subjects addressed in this book, making them accessible to EJB novices, and the breadth of material covered in the volume do make up for its weightiness."

Read the detailed review at JavaLobby