Archive

Posts Tagged ‘jpa’

Injecting JPA EntityManager in Google Guice through JBoss Seam

July 31st, 2009 Shrihari No comments

This cool tip describes a 3 step process to use JPA’s EntityManager initialized in JBoss Seam inside the Google Guice environment.

1). Define Seam’s components.xml with entity-manager-factory component as below

<components>
    <persistence:entity-manager-factory name="my_persistence_unit"/>
    <guice:init injector="#{myInjector}"/>
    <guice:injector name="myInjector">
        <guice:modules>
           <value>mypackage.MyEntityManagerModule</value>
        </guice:modules>
    </guice:injector>
</components>

where my_persistence_unit is the persistence unit name under META-INF/persistence.xml.

2). Define a Google Guice Module to wire-up Seam’s EnitiyManagerFactory by looking-up based on the expression-value and binding it to Guice context using a Provider

public class MyEntityManagerModule extends AbstractModule
{   public void configure()
    {   Expressions expressions = Expressions.instance();
        ValueExpression emfVE = expressions.createValueExpression("#{my_persistence_unit}");
        EntityManagerFactory emf = (EntityManagerFactory)emfVE.getValue();
        EntityManagerProvider.setEntityManagerFactory(emf);
        bind(EntityManager.class).toProvider(EntityManagerProvider.class).in(Scopes.SINGLETON);
     }
 }
 
class EntityManagerProvider implements Provider
    {   static EntityManagerFactory entityManagerFactory = null;
        public EntityManager get()
        {   return entityManagerFactory.createEntityManager();
        }
    }
 

3) One could use the EntityManager anywhere in the Seam-Guice hybrid component:

@Name("hybrid")
@Guice
public class HybridDAO
{  @Inject EntityManager entityManager;
   //other DAO methods.
}

This makes Guice components use EntityManager normally use Seam established EntityManagerFactory

Categories: jee-light Tags: , ,

Implementing a Unitils DAO testing infrastructure for Spring and JPA on TestNG

June 29th, 2009 Shrihari No comments

Unitils provides an excellant infrastructure for unit testing DAO (and data-service) layers  for Spring as IoC Framework on all the component unit testing frameworks such as JUnit 3, JUnit4 and TestNG, without any boilerplate code or coniguration hassles. We will look at establishing a testing infrastructure for Spring with JPA on TestNG frmaework.

Suppose the custom DAO and its implementation definition are like this (with regard to semantics of Spring JPA integration)

package mypackage;
public CustomDAO
{    public List findAll();
     public void save(Entity eo);
}
package mypackage;
public CustomDAOImpl implements CustommmDAO
{   @PersistenceContext
    private EntityManager em;
    //all other method implementations..
      ....
}

In order to setup a Unitils test infrastructure on TestNG, the following is the class-template:

public class CustomDAOTestCase extends UnitilsTestNG
{   /**
     * Injects a test specific application context configuration
     */
    @SpringApplicationContext
    public ConfigurableApplicationContext createApplicationContext()
    {  return new ClassPathXmlApplicationContext("applicationContext-test.xml");
    }
     @SpringBean("customDAO")
     private CustomDAO customDAO;
     //all @Test annotated methods..
      @Override
      protected void unitilsAfterTestTearDown(java.lang.reflect.Method method)
      {}
}

for which the following would be the test specific Spring’s application context confguration (applicationContext-test.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="entityManagerFactory" >
 <property name="persistenceXmlLocation" value="persistence-test.xml"/>
 <property name="persistenceUnitName" value="customservice-test"/>
 </bean>

 <bean id="transactionManager">
 <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>

 <tx:annotation-driven />

 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

 <bean id="customDAO" class="mypackage.CustomDAOImpl"/>
</beans>

Look specifically at entityManagerFactory bean definition, org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean allows us to load test specific persistence.xml which could allow us to maintain more than onne persistence context for a sample application.
These two definitions extend the Spring’s JPA capabilities for unit testing DAO on Unitils infrastructure!

Categories: Uncategorized Tags: , ,