From 59d4e800108bd2771f73bc66be0dc77d95654e6c 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 0d5dacd05..9699a63e1 100644 --- a/src/docbkx/jpa.xml +++ b/src/docbkx/jpa.xml @@ -1270,7 +1270,8 @@ class Config { You can now set up the infrastructure by implementing a CDI Producer for the - EntityManagerFactory: + EntityManagerFactory and + EntityManager: class EntityManagerFactoryProducer { @@ -1283,8 +1284,36 @@ class Config { 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