Hibernate – Creating a JPA EntityMananger in Spring Context

I have a JPA/Spring application which uses Hibernate as the JPA provider. In part of the code, I have to manually create DAOs in my application using the new operator, Instead of using Spring DI. When I do this, Spring will not process the @PersistenceContext annotation.

In the code where I create the DAO, I have an EntityManagerFactory, which I use to set the entityManager, As follows:

@PersistenceUnit
private EntityManagerFactory entityManagerFactory;

MyDAO dao = new MyDAOImpl();
dao. setEntityManager(entityManagerFactory.createEntityManager());

The problem is that when I do this, I get a Hibernate error:

Could not find UserTransaction in JNDI [java:comp/UserTransaction]

This is the spring entity manager factory configuration:










org.hibernate.transaction.JTATransactionFactory
com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup







It has nothing to do with EntityManager. This is a transaction configuration issue. Obviously, org.hibern ate.transaction.JTATransactionFactory forces a JNDI lookup.

Switch to com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory, everything is normal.

< p>I have a JPA/Spring application that uses Hibernate as the JPA provider. In a part of the code, I have to manually create DAOs in my application using the new operator instead of using Spring DI. When I do this When doing this, Spring will not process the @PersistenceContext annotation.

In the code where I create the DAO, I have an EntityManagerFactory, and I use it to set the entityManager as follows:

< p>

@PersistenceUnit
private EntityManagerFactory entityManagerFactory;

MyDAO dao = new MyDAOImpl();
dao.setEntityManager(entityManagerFactory.createEntityManager());

The problem is that when I do this, I get a Hibernate error:

Could not find UserTransaction in JNDI [java:comp/UserTransaction]< /pre> 

This is the spring entity manager factory configuration:

 








org.hibernate.transaction.JTATransactionFactory
com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup







It has nothing to do with EntityManager. This is a transaction configuration issue. Obviously, org.hibernate.transaction.JTATransactionFactory forces JNDI lookups.

< /p>

Switch to com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory, everything is normal.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4460 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.