Posted by dibyendumajumdar
on February 14, 2006 at 3:22 AM PST
I'd like to canvas your views about how to package entity beans. My objectives are to achieve reusability so that I can deploy the entities in different environments such as:
1. J2EE containers
2. Servlet containers
3. J2EE clients
4. J2SE environments
Most examples you come across package the entity beans together with the rest of the system in a single EAR. However, this is not the best way of achieving flexibility and reusability, as it is easy to build dependencies between the persistence layer and the rest of the system.
I have been experimenting with two options:
1. Entities have their own independent project, and are packaged in a jar file along with the persistence.xml file.
2. A set of Enterprise Beans are defined to act as the Data Access layer. These work with entities and manage persistence. The idea is that the Data Access layer hides details of how the entities are persisted, and it should be possible in theory to implement pure JDBC code here rather than using EJB 3.0 persistence. Anyway, the EJBs are packaged in a separate jar file and have their own independent project.
In this option, I combine the entities and the corresponding EJBs in the same package/project.
Now, my question is, which of these two options would you prefer and why?
Secondly, is it a worthwhile goal to allow the entities to be reusable outside the J2EE container? I think it is, but the issue is that to be truly reusable, you must avoid depending upon functionality that is specific to J2EE containers.