• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName DemoJPA  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I am getting this error when I try to use entity from servlet

java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName DemoJPA


what is the root cause for this error


here is my servlet code

package example.client;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import example.entities.Manufacturer;

/**
* Servlet implementation class TestJPA
*/
public class TestJPA extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public TestJPA() {
super();
// TODO Auto-generated constructor stub
}

@PersistenceContext (unitName="DemoJPA")
EntityManager em;

Manufacturer man;

protected void populateManufacturer(){
man = new Manufacturer();
man.setAddressline1("address1");
man.setAddressline2("address2");
man.setCity("bangalore");
man.setEmail("abe@gmail.com");
man.setFax("1234");
man.setName("Ram");
man.setPhone("567");
man.setRep("RK");
man.setState("Karnatka");
man.setZip("001");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
populateManufacturer();
em.persist(man);

}

}


And persitence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="DemoJPA">
<class>example.entities.Manufacturer</class>
</persistence-unit>
</persistence>



Any help is really appretiated

Thanks


 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which container are you using?

is the datasource properly set?
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using glassfish3, maven and NetBeans I experienced this problem too. Finally it was solved by declaring stateless bean with @Startup annotation.My project is from chapter 10, "Beginning JavaEE6 with GlassFish3+" by Antonio Goncalves.
@Stateless
@Startup
@LocalBean

//insted of public class BookEJB implements BookEJBRemote{
//it is needed to set now
public class BookEJB {
@PersistenceContext(unitName = "com.testingPU")
EntityManager em;

public BookEJB(){}
//methods
}
 
Ranch Hand
Posts: 55
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Volodymyr Levytskyi wrote:Using glassfish3, maven and NetBeans I experienced this problem too. Finally it was solved by declaring stateless bean with @Startup annotation.My project is from chapter 10, "Beginning JavaEE6 with GlassFish3+" by Antonio Goncalves.
@Stateless
@Startup
@LocalBean

//insted of public class BookEJB implements BookEJBRemote{
//it is needed to set now
public class BookEJB {
@PersistenceContext(unitName = "com.testingPU")
EntityManager em;

public BookEJB(){}
//methods
}



@Startup is only for Singleton.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a similar issue with Embedded Glassfish server,

Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName cis_ejbPU
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:132)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:173)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.unwrap(EntityManagerWrapper.java:1072)
at com.hsntech.cis.eao.impl.PropGridEaoImpl.getPropertySet(PropGridEaoImpl.java:210)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5367)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:801)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:862)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:801)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:371)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5339)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5327)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
... 36 more

Apr 19, 2016 5:11:28 PM org.glassfish.admin.mbeanserver.JMXStartupService shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
Apr 19, 2016 5:11:28 PM com.sun.enterprise.v3.server.AppServerStartup stop
INFO: Shutdown procedure finished
Apr 19, 2016 5:11:28 PM AppServerStartup run
INFO: [Thread[GlassFish Kernel Main Thread,5,main]] exiting
Closing the container

TO RESOLVE IT I DID THE FOLLOWING

1) In the pom.xml add the following dependencies
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.2-b06</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

2) Set up the HashMap properties for the embedded container
File target = prepareModuleDirectory();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(EJBContainer.MODULES, target);

properties.put("org.glassfish.ejb.embedded.glassfish.installation.root", "./src/test/glassfish/");
properties.put("org.glassfish.ejb.embedded.glassfish.configuration.file", "./src/test/glassfish/domains/domain1/config/domain.xml");
container = javax.ejb.embeddable.EJBContainer.createEJBContainer(properties);

3)
private static final String MODULE_NAME = "embedded";
private static final String TARGET_DIR = "target/" + MODULE_NAME;


private static File prepareModuleDirectory() throws IOException {
File result = new File(TARGET_DIR);
FileUtils.copyDirectory(new File("target/classes"), result);
FileUtils.copyFile(new File("target/test-classes/META-INF/persistence.xml"),
new File(TARGET_DIR + "/META-INF/persistence.xml"));
return result;
}

4) create the foolder structure under src\test\glassfish\domains\domain1\config and copy the glassfish domain.xml which is present under <glassfish server install> glassfish\domains\domain1\config to src\test\glassfish\domains\domain1\config.

I followed the above steps to resolve the issue.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!