Issue
What do I want to do:
I just started with JPA and therefore i want to understand how persisting Entitys from different perspectives work (Application-Managed, Container-Managed). Application-Managed works fine, however i have problems persisting an Entity with an Entity Manager from within a Web Application using a TestServlet.
What have I done so far:
IDE: NetBeans 8.0.2 Server: GlassFish 4.1 Mysql 5.6 JPA 2.1: EclipseLink
- I've created a Web Application Project.
- I've imported the Entitys from the Database using the NetBeans Wizard. For some reason I am not able to import them from jdbc/__default. I have to create a new Data Source. In this case it was jdbc/__onlineshop
I simply want to create an Entity within a Servlet and persist it in the database just to understand how it works before I move on but apparently I don't quite get it.
I am connected to the database and i can access it via a Java-Application but not using a Servlet from a Web-Container.
The MySQL Driver is in the classpath MAIN - Libraries.
No Exceptions are thrown. The Servlet is called which I am able to see in the browser. However the redirection through the RequestDispatcher does not work either, the browsers URL stays at web-jpa/test.
My Question:
Is there anything wrong with my Servlet or the persistence.xml since persisting does not work ?
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/ persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="jpa-webPU" transaction-type="JTA">
<jta-data-source>jdbc/__onlineshop</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
TestServlet.java :
package jpa;
import java.io.IOException;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
@WebServlet("/test")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@PersistenceUnit
EntityManagerFactory emf;
@Resource
private UserTransaction ut;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EntityManager em = emf.createEntityManager();
Customer customer = new Customer();
customer.setEmail("[email protected]");
customer.setPassword("password");
try
{
ut.begin();
em.persist(customer);
ut.commit();
} catch (Exception e) {
try
{
ut.rollback();
} catch (Exception e1)
{
e1.printStackTrace();
}
throw new ServletException(e.getMessage());
}finally
{
em.close();
}
RequestDispatcher dispatcher = request.getRequestDispatcher ("index.html");
dispatcher.forward(request, response);
}
}
Thank you !
p.s.: Any Hint is appreciated !
Solution
Finally I have found a solution:
try
{
ut.begin();
em.joinTransaction(); // this is necessary, the entitymanager needs to be called specifically
em.persist(customer);
ut.commit();
}
Answered By - SklogW
Answer Checked By - David Goodson (JavaFixing Volunteer)