[jboss-user] [EJB 3.0] - Manually created Hibernate EntityManager vs. injected one

alexeinov do-not-reply at jboss.com
Wed Nov 12 11:19:37 EST 2008


Because of isolated classloading problems with several applications in the same container I have to avoid injecting EntityManager via a @PersistenceContext annotation. 

I tried to use the following code in my stateless bean:

  |     private EntityManager em;
  | 
  |     @PostConstruct
  |     public void injectEntityManager() {
  |       EntityManagerFactory emf = Persistence.createEntityManagerFactory("em");
  |       em = emf.createEntityManager();
  |     }
  | 
It works perfectly. I can see that a JTA type transaction is activated when I request a transaction using @TransactionAttribute and @TransactionManagement annotations. So far so good.

My question is: do I miss something if I create an EntityManager manually? Are there any gotchas doing it this way? When I try to trace call hierarchies in a debugger, I can see that they are very different, and involve different classes.

Manual bootstrapping of EntityManager via the Persistence class is only scarcely mentioned in Hibernate and JBoss documentation in context of a Java SE application.

Can somebody comment on if it is OK to go this way within JBoss container?

thanks!
/Alexei

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4188844#4188844

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4188844



More information about the jboss-user mailing list