views:

30

answers:

1

There are few applications that use java persistence. Each application has its own set of entities that represents the same tables in DB. For instance, application A has entities for Table1, Table2, etc; at the same time, application B also has entities for Table1, Table2, etc. Instead of that I want to create a new EJB module with 1 bean with local interface, move all entities into it, and add it as a library to projects that may require access to persistent objects. So, it will look like

@Stateless
public class DataBean implements DataLocal {        
@PersistenceContext(unitName="my_data")
private EntityManager em ;  
public EntityManager getManager()
{
  return em;
};
}

I'm pretty new in Java-ee, so I wonder whether it is a poor design or not. Thanks in advance.

+3  A: 

There are few applications that use java persistence.

Are you kidding? :)

Instead of that I want to create a new EJB module with 1 bean with local interface, move all entities into it, and add it as a library to projects that may require access to persistent objects.

Putting Entities in separated JARs is definitely the right approach for reuse and modularity. And to use such packaged entities, use the jar-file element in the persistence.xml to make them visible to the packaged persistence unit of each application needing them.

I'm pretty new in Java-ee, so I wonder whether it is a poor design or not. Thanks in advance.

I don't get the point of the stateless session bean and the getter returning an EntityManager, you don't need that in my opinion.

Pascal Thivent
What I wanted is not to change the existing sources too much. At the current each application has a stateless bean that uses resource injection to get EntityManger. My idea was to inject DataBean instead and call getManager() whenever I need it.
a1ex07
@a1ex07 I don't see the difference at the end and the applications will very likely need an EntityManager anyway. So it's actually more work and more verbose IMHO. But maybe I'm missing something.
Pascal Thivent
I'm just considering different ways... I totally forgot that persistence.xml can contain jar-file section, thanks for pointing that out.
a1ex07