| Author |
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName DemoJPA
|
ramakrishna kulkarni
Greenhorn
Joined: Feb 06, 2009
Posts: 14
|
|
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
|
 |
Justin Russo
Ranch Hand
Joined: Oct 21, 2007
Posts: 77
|
|
which container are you using?
is the datasource properly set?
|
You Want it.. Get it.......the Right Way...<br /> <br />SCJP 5.0 SCWCD 5.0
|
 |
Volodymyr Levytskyi
Ranch Hand
Joined: Mar 29, 2012
Posts: 187
|
|
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
}
|
 |
Cosmin Nicolae Vacaroiu
Ranch Hand
Joined: Feb 10, 2011
Posts: 51
|
|
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.
|
SCJP 6 (93%), SQL Expert 11g (95%), SCWCD 6 (84%), OCE-EJBD 6 (98%), OCE-JPAD 6 (93%)
|
 |
 |
|
|
subject: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName DemoJPA
|
|
|