Archive for April 2012

Saving development time with local EntityManager in JPA

In almost all real world projects (non Spring + We are not considering Arquillian or any similar framework) where you are using JPA (Hibernate implementation), the data source binding happens by JNDI and the services are provided by EJBs, which means to test your entities for any CRUD operation you have to deploy the bundle (ear, ejb or war) into the application server which adds time to your development efforts each time you make any change. So in this post we will see how we can temporarily inject entity manager into your business class so that you can test all the operations you want to perform on entities without deploying the application or restarting server quite often.

persistence.xml: The configuration for entity managers both inside an application server and in a standalone application reside in a persistence archive. In the next section we focus on standalone configuration.


Please Note:
  • The database dialect we have chosen is MySQL for obvious reasons.
  • Persistence unit has name test_unit and the type is RESOURCE_LOCAL and not JTA.
  • com.gognamunish.test.Entity is the entity which we want to test later.
  • As we are running without a JNDI available datasource, we must specify JDBC connections with Hibernate specific properties
Next in your business class you can get reference to the entity manager as follow:
EntityManagerFactory factory ;
factory = Persistence.createEntityManagerFactory("test_unit");
EntityManager manager = factory.createEntityManager();
That's all you need , after obtaining the reference focus only on your entity and the operation you want to perform on it.
Let's try one sample example:
/* Sample Entity class */
@Table(name = "Entity")
public class Entity {
 private Long id;
 private String value;
package com.gognamunish.test;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

 * Sample Test - nothing fancy.
 * @author Munish Gogna
public class LocalEntityManager {
 static EntityManager manager;
 static {
  EntityManagerFactory factory = Persistence
  manager = factory.createEntityManager();

 public static void main(String[] args) {

  System.out.println("Before Entities: " + getEntities());

  Entity entity = new Entity();
  entity.setValue("my first value");

  System.out.println("After Entities: " + getEntities());


 private static Entity saveOrUpdate(Entity entity) {
  return manager.merge(entity);

 private static List<Entity> getEntities() {
  return manager.createQuery("from Entity").getResultList();

Hibernate: select as id0_, entity0_.value as value0_ from Entity entity0_
Before Entities: []
Hibernate: select as id0_0_, entity0_.value as value0_0_ from Entity entity0_ where
Hibernate: insert into Entity (value, id) values (?, ?)
Hibernate: select as id0_, entity0_.value as value0_ from Entity entity0_
After Entities: [Id:1, Value:my first value]
Sunday, April 29, 2012
Posted by Munish Gogna

Popular Post


enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)