File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ramakrishna kulkarni
Greenhorn
Posts: 14
  • 0
  • 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


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

is the datasource properly set?
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • 0
  • 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
}
 
Cosmin Nicolae Vacaroiu
Ranch Hand
Posts: 55
Eclipse IDE Java Oracle
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic