-1

I try to use Spring+JPA+Hibernate and try to inject EntityManagerFactory,and later create EntityManger in my code.But when I use entityManager.persist(user),the user not saving to the database.But when I try to inject the EntityManager instead of EntityManagerFactory,it worked !,I do not know where is the problem.

you can also see this question for more code.

Community
  • 1
  • 1
TIMFUNNY
  • 363
  • 4
  • 9
  • 26

1 Answers1

1

When using a plain EntityManagerFactory instead of an EntityManager you need to call createEntityManager. This will always create a new EntityManager, this is basically a plain EntityManager not managed nor detected by Spring. So you will also have to start/commit transactions yourself.

When using the EntityManager you will obtain a transactional synchronized instance, which is managed by Spring and bound to the current transaction. So no need to start / commit a transaction yourself.

See also the JPA section of the reference guide.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224