Home > Uncategorized > Using Google Guice to inject JPA EntityManager

Using Google Guice to inject JPA EntityManager

I am working on an persistence example using PrimeFaces, Google Guice and Hibernate. Following are the four steps to inject a JPA EntityManager into the Manager  (Appfuse parlance) / DAO Layer:

1) Define META-INF/persistence.xml containing JPA Configuration (similar to hibernate.cfg.xml)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
 version="1.0">

 <persistence-unit name="persdb" transaction-type="RESOURCE_LOCAL">
 <provider>org.hibernate.ejb.HibernatePersistence</provider>
 <properties>
 ...
 </properties>
 </persistence-unit>

</persistence>

2) Create a startup controller Guice bean

@Controller(name="startupBean", startup=true)
public class StartupBean
{    public StartupBean()
      {}
}

3) Create a Google Guice Module

public class MyModule implements Module
{       public void configure(Binder binder)
          {   AnnotatedBindingBuilder bindingBuilder = binder.bind(MyDataService.class);
              ScopedBindingBuilder scopedBuilder = bindingBuilder.to(MyDataServiceImpl.class);
              scopedBuilder.in(SINGLETON);
          }
}

and create a context parameter in WEB-INF/web.xml

<context-param>
 <param-name>optimus.CONFIG_MODULES</param-name>
 <param-value>mypackage.MyModule,org.primefaces.optimus.persistence.JPAModule
 </param-value>
 </context-param>

Note: org.primefaces.optimus.persistence.JPAModule actually injects the EntityManager reading the META-INF/persistence.xml

4) Create the DaraService implementation containing the injected EntityManager

public class MyDataServiceImpl implements MyDataService
{        @Inject
            private EntityManager em;
           //all methods using JPA entityManager here

}

No other configuration xmls to be changed. Ain’t it cool!

Categories: Uncategorized Tags:
  1. June 24th, 2009 at 19:32 | #1

    BTW you can use the standard EJB @PersistenceContext annotation if you prefer, using GuiceyFruit…

    http://code.google.com/p/guiceyfruit/

  2. shriharisc
    June 24th, 2009 at 23:25 | #2

    Thanks James. Would look into GuiceyFruit once the pattern is evolved.
    ~Shrihari

  3. June 27th, 2009 at 15:38 | #3

    Hi, I’m the author of PrimeFaces and for some time I’m really considering dropping the proof of concept jpa integration work and integrate guiceyfruit. Let’s see how it goes.

  4. Ronak Patel
    July 15th, 2009 at 07:04 | #4

    James,

    I can’t figure out how to use Guiceyfruit with Hibernate…it always complains that nothing is bound to the EntityManager.class interface…then I try to bind the Hibernate implementation of the EntityManager class.

    I get another error then saying that there is no no-arg constructor for Hibernate’s EntityManagerImpl. How do I use Guiceyfruit correctly??

  5. shriharisc
    July 16th, 2009 at 18:25 | #5

    Hi Ronak,
    Actually your META-INF/persistence.xml should be in your classpath and you would need to bind the org.guiceyfruit.jpa.JpaModule to your Guice Injection context. I could post you a sample example if you need.
    Thanks
    Shrihari

  1. No trackbacks yet.