Is it possible to use @Transational worked in Spri

2019-08-28 08:55发布

问题:

HiAll, I was confused by the EclipseLink for Tomcat documentation saying:

Limitations to JPA: No @PersistenceContext injection of a container managed persistence unit is available - use Persistence.createEntityManagerFactory(JTA_PU_NAME)

and, also by this question&answer:

but the typical JPA configuration in Spring looks like this, so you don't need to create EntityManager manually : @PersistenceContext private EntityManager em;

So, the question is: may I use this annotation @PersistenceContext for the entityManager to be sure that it will be created automatically and I dont need to create it manually?

Currently my @Transactional annotation does not work properly and I'm afraid it is because of creating the entity manager manually!

Please, need help.

回答1:

EclipseLink documentation says that @PersistenceContext is not handled by Tomcat on its own. However, if you use Spring, @PersistenceContext is handled by Spring, so that you can use it.

See also:

  • 13.5 JPA