Skip to Content
Author's profile photo Former Member

Tutorial: Put JPA in your Web App (tomcat, EclipseLink) and provide Rest JSON output

Here is a short and simple example of how to use JPA outside of EJB container and provide JSON rest output.

As a JPA Provider I selected EclipseLink as it is a reference implementation, but I am sure that Hibernate and other stuff can easily replace EclipseLink. For JAX-RS I used Jersey. Tomcat was my server to test on.

Project structure.

As you can see from screenshot below there are only 5 valuable files and the structure of the project – the typical web project, generating WAR deployment archive. So the project structure for maven build will be a bit different. There are some point here – persistence.xml should be located so class loader can pick it up. So it is placed directly under source folder, under META-INF folder, so we have two META-INF directories in the project.


Creating entities.

I created the only one entity as an example:

package com.maxud.model;
import javax.persistence.*;
@Table(name = "user_")
public class Employee {
          @Column(name = "userId")
          private Long id;
          @Column(name = "firstName")
          private String firstName;
          @Column(name = "lastName")
          private String lastName;
          public long getId() {
                    return id;
          public void setId(long id) {
           = id;
          public String getFirstName() {
                    return firstName;
          public void setFirstName(String firstName) {
                    this.firstName = firstName;
          public String getLastName() {
                    return lastName;
          public void setLastName(String lastName) {
                    this.lastName = lastName;

Describing persistence.xml

We don’t want to make the difficult setup, so database connection settings are placed here. Below is the file itself, main points here are:

  • Persistence Unit name
  • JPA entity provider, in this case EclipseLink
  • List of entity classes
  • jdbc connection settings
  • EclipseLink related properties
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
          <persistence-unit name="EmployeePU" transaction-type="RESOURCE_LOCAL">
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/portal?useUnicode=true&amp;characterEncoding=UTF-8"/>
      <property name="javax.persistence.jdbc.user" value="portal"/>
      <property name="javax.persistence.jdbc.password" value="portal"/>
          <!-- EclipseLink should create the database schema automatically -->
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.ddl-generation.output-mode" value="database" /> 

Deployment descriptor web.xml

All stuff is quite important here:

  • Including Jersey servlet taking care of Rest Services (listens specific package) and parameter, producing the json output
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-name>Jersey REST Service</servlet-name>

Adding WebListener, taking care of JPA initialization:

It is taking care of persistence unit initialization so you can get the EntityManager whenever you want from other part of your application.

package com.maxud.listener;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
public class LocalEntityManagerFactory implements ServletContextListener{
    private static EntityManagerFactory emf;
    public void contextInitialized(ServletContextEvent event) {
        emf = Persistence.createEntityManagerFactory("EmployeePU");
    public void contextDestroyed(ServletContextEvent event) {
    public static EntityManager createEntityManager() {
        if (emf == null) {
            throw new IllegalStateException("Context is not initialized yet.");
        return emf.createEntityManager();

Web Controller, Web Service and JSON output

There are some points here:

  • Rest service can be call by GET HTTP method, using path /employee/412731
  • 412731 is used as parameter to read() method
  • we are taking EntityManager, make call to database
  • Method return Employee type which is transformed to JSON because of @Produces(MediaType.APPLICATION_JSON) annotation
  • btw, you can easily access database even without deployment to web container, see main() method as an example.
package com.maxud.controller;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.maxud.listener.LocalEntityManagerFactory;
import com.maxud.model.Employee;
public class EmployeeController {
    public Employee read(@PathParam("id") long id) {
                    long start = System.currentTimeMillis();
                    System.out.println(" started");
                    EntityManager em = LocalEntityManagerFactory.createEntityManager();
                    try {
                              return em.find(Employee.class, id);
                    } finally {
                              System.out.println("Getting data took " + (System.currentTimeMillis() - start) + "ms.");
          public static void main(String ...args) {
                    Map props = new HashMap();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeePU", props);
        Employee emp = emf.createEntityManager().find(Employee.class, new Long(23411));
        if (emp != null) {
                  System.out.println("Your user is: " + emp.getFirstName() + " " + emp.getLastName());

That’s it.

Best wishes,


Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Vladimir Yurishchev
      Vladimir Yurishchev

      Very consice and useful article, +1 from my side!

      Author's profile photo Former Member
      Former Member

      Useful article but I think it would be good if a reader could see the result of using of this set of objects.