views:

63

answers:

1

There are 2 ways of using a persistence unit, code or annotation.

CODE
[..]
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("SOMEPU");
[..]

or

ANNOTATION
[..]
@PersistenceContext(name = "persistence/LogicalName", unitName = "SOMEPU")
[..]

Question: If you want to change the persistence unit (or point to different jdbc source), I could easily adapt the sourcecode version to read a variable from some settings file or whatever. But I cant put variables into annotations. Whats is the solution ?

Yes, I could keep always the same PU and just point the jbdc resource in the applicationserver to somewhere else, but I dont want anyone to tinker around in the admin settings of the AS.

cheers Sven

A: 

If you absolutely have to use annotations to get your PersistenceContext, then I guess you could wrap the creation of the EntityManager in some class and then have that injected into the bean that requires it?

public interface MyPersistenceContext
{
      public void getEntityManager();
}

And then in your EJB:

public class MyEJB
{

      @EJB
      private MyPersistenceContext persistenceContext;

      private EntityManager em;

      @PostConstruct
      public void postConstruct()
      {
             em = persistenceContext.getEntityManager();
      }

 ....

How the implementation of MyPersistenceContext you provide creates the EntityManager is up to you.