I am attempting to build a brand new Spring Framework 4.0 project without all of the magical gradle stuff, but simply kicking it old school.
I am following the tutor
The EntityManager
interface belongs to JPA and is implemented by JPA providers (such as Eclipse), not Spring, and it has its own injection annotation: @PersistenceContext
. EntityManager
objects are transaction-scoped and should not be exposed as beans as you're doing. Instead, either use the JPA annotation to inject the EntityManager
:
@PersistenceContext
EntityManager em;
or, since it looks like you're trying to use Spring repositories, inject the repository instead:
@Autowired
PersonRepository pr;
though this article is pretty old and has already bean answered, i encountered the same problem with the oficial turorial spring-data-jpa on spring-io website.
the current spring data jpa example/spring io guide detects at least 2 beans implementing the interface "EntityManager". You must help spring what implementation to wire to. I recommend to add a qualifier.
In class JPAConfiguration add:
@Bean
@Qualifier(value = "entityManager")
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
In the test class add:
@Autowired
@Qualifier(value = "entityManager")
EntityManager manager;
Hope it helps you.
I would not necessarily use the "PersistenceContext", as spring handles the creation of transaction manager, entitymanager, beans, datasource etc.. "PersisenceConetext" annotation is basically defined for container managed entityfactory of an application server/ejb compliant container