From 8022342188e20d0cbcb7a13aa06ed9d0f4f1f8d0 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sun, 23 Feb 2014 16:07:51 +0100 Subject: [PATCH] DATAJPA-474 - Fixed documentation on CDI setup of EntityManagers. Previously, the reference documentation was missing details of how to set up EntityManager instances for usage of Spring Data JPA repositories in a CDI context. Augmented the section on complete manual setup but also added an example of the necessary setup in a pure JavaEE context. --- src/docbkx/jpa.xml | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/docbkx/jpa.xml b/src/docbkx/jpa.xml index 799e2b93a..429beb626 100644 --- a/src/docbkx/jpa.xml +++ b/src/docbkx/jpa.xml @@ -1260,7 +1260,8 @@ public interface UserRepository extends JpaRepository<User, Long> { You can now set up the infrastructure by implementing a CDI Producer for the - EntityManagerFactory: + EntityManagerFactory and + EntityManager: class EntityManagerFactoryProducer { @@ -1273,8 +1274,36 @@ public interface UserRepository extends JpaRepository<User, Long> { public void close(@Disposes EntityManagerFactory entityManagerFactory) { entityManagerFactory.close(); } + + @Produces + @RequestScoped + public EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) { + return entityManagerFactory.createEntityManager(); + } + + public void close(@Disposes EntityManager entityManager) { + entityManager.close(); + } } + The necessary setup can vary depending on the JavaEE environment + you run in. It might also just be enough to redeclare a + EntityManager as CDI bean as + follows: + + class CdiConfig { + + @Produces + @RequestScoped + @PersistenceContext + public EntityManager entityManager; +} + + In this example, the container has to be capable of creating JPA + EntityManagers itself. All the + configuration does is re-exporting the JPA + EntityManager as CDI bean. + The Spring Data JPA CDI extension will pick up all EntityManagers availables as CDI beans and create a proxy for a Spring Data repository whenever an bean of a