2

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.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
a1ex07
  • 36,826
  • 12
  • 90
  • 103

1 Answers1

4

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
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 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 Feb 08 '10 at 16:10
  • @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 Feb 08 '10 at 19:36
  • I'm just considering different ways... I totally forgot that persistence.xml can contain jar-file section, thanks for pointing that out. – a1ex07 Feb 09 '10 at 16:45