How to get rid of Transfer Objects with EJB3 JPA? (Hint: Deprecate the Stateless Architecture!)
Friday, October 13th, 2006Diego Parrilla recently wrote in his blog: “Yes, we still need Transfer Objects with EJB3 JPA”. I think he raised a lot of interesting and valid pionts on the issues we face using EJB3 entity bean POJOs in the web tier. However, I think the answer is also simple: Use a web framework that is designed for the ORM backend! I understand that it is not entirely feasible since many developers have no control over what web framework they choose. But if you do, using a stateful framework like JBoss Seam would completely eliminate the need of DTOs and save you A LOT of trouble.
One of the biggest complaints Diego had was that the web tier cannot lazy load associated objects once the transaction is finished in the business tier. And that forces him to eager load everything and causes a lot of management / performance problems. In fact, this is also one of the most common complaints we heard from Spring / Hibernate users. The problem is that business tier is “stateless” and it closes the database transaction after it passes the object off to the web tier — hence preventing lazy loading in the web tier.
The extended persistence context in EJB3 is designed to solve this problem (yes, the “experts” did think of this :)). With the extended persistence context, the ORM POJO’s lifecycle is extended beyond the business layer transaction. So, you can lazy load anything as long as you do not destroy the context. But it is only useful when used in a EJB3 stateful session bean, which holds the context. JBoss Seam supports EJB3 stateful session bean as one of its basic constructs, and therefore supports lazy loading in web tier out-of-the-box.
Check it out!