Using JPA in REST web application deployed in Jetty

I wanted to figure out how to use Hibernate JPA in a web application deployed in Jetty server, so i built this sample Contact REST service that uses JPA to perform CRUD operations on the CONTACT table and expose those operations using REST, you can download it from here I followed these steps to build the REST service.
  1. First i did create a Contact.java class which is a Entity class representing CONTACT table
    
    package com.webspherenotes.rest;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @Entity
    @XmlRootElement
    public class Contact {
     @Id
     private int contactId;
    
     private String firstName;
     private String lastName;
     private String email;
     
     public int getContactId() {
      return contactId;
     }
     public void setContactId(int contactId) {
      this.contactId = contactId;
     }
     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;
     }
     public String getEmail() {
      return email;
     }
     public void setEmail(String email) {
      this.email = email;
     }
     
     @Override
     public String toString() {
      return "Contact [contactId=" + contactId + ", firstName=" + firstName
        + ", lastName=" + lastName + ", email=" + email + "]";
     }
    }
    
    The contactId field represents the primary key for contact table. The Contact class has Entity annotation that maps it to table in database.
  2. Then i did create peristence.xml file which acts as deployment descriptor for the JPA in the META-INF folder of the application like this
    
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.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_2_0.xsd">
     <persistence-unit name="MyJPAJAXRS">
      <class>com.webspherenotes.rest.Contact</class>
      <properties>
       <property name="javax.persistence.jdbc.driver" 
       value="org.apache.derby.jdbc.ClientDriver" />
       <property name="javax.persistence.jdbc.url" 
       value="jdbc:derby://localhost:1527/C:/data/contact;create=true" />
       <property name="javax.persistence.jdbc.user" value="dbadmin" />
       <property name="javax.persistence.jdbc.password" value="dbadmin" />
      </properties>
     </persistence-unit>
    </persistence>
    
    The persistence.xml file is using JDBC driver properties for connecting to Apache Derby database.
  3. Create ContactApplication.java class which be the Application class for this REST application
    
    package com.webspherenotes.rest;
    javascript:void(0);
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    @ApplicationPath("rest")
    public class ContactApplication extends Application{
      Logger logger = LoggerFactory.getLogger(ContactApplication.class);
    
      @Override
      public Set<Object> getSingletons() {
        logger.debug("Entering ContactApplication.getSingletons()");
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyJPAJAXRS");
        ContactService contactService = new ContactService(entityManagerFactory);
        Set<Object> singletons = new HashSet<Object>();
        singletons.add(contactService);
        logger.debug("Exiting ContactApplication.getSingletons()");
        return singletons;
      }
    
    }
    
    The ContactApplication class overrides getSingletons() method so that it can create single instance of ContactService, which will be responsible for handling all the requests for ContactService. Since Jersey in not JEE 6 container i am responsible for creating object of EntityManagerFactory. After creating object of EntityManagerFactory i am injecting it into the ContactService class.
  4. The last step is to create ContactService.java, which exposes the REST operations
    
    package com.webspherenotes.rest;
    
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Query;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.DELETE;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    @Path("/contact")
    public class ContactService {
    
      Logger logger = LoggerFactory.getLogger(ContactService.class);
      
      EntityManagerFactory entityManagerFactory;
      public ContactService(EntityManagerFactory entityManagerFactory){
        this.entityManagerFactory=entityManagerFactory;
      }
    
      @GET
      @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
      public List getContactList() {
        logger.debug("Entering ContactService.getContactList()");
    
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Query q = entityManager.createQuery("SELECT x from Contact x");
        logger.debug("Exiting ContactService.getContactList()");
    
        return (List) q.getResultList();
      }
    
      @GET
      @Path("/{contactId}")
      @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
      public Contact getContact(@PathParam("contactId") int contactId) {
        logger.debug("Entering ContactService.getContact() contactId" + contactId);
    
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Contact contact = entityManager.find(Contact.class, contactId);
        logger.debug("Exiting ContactService.getContact()" );
    
        return contact;
      }
      
      @POST
      @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
      @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
      public Contact insertContact(@FormParam("contactId") int contactId,
          @FormParam("firstName") String firstName,
          @FormParam("lastName") String lastName,
          @FormParam("email") String email) {
        logger.debug("Entering ContactService.insertContact()");
    
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Contact contact = new Contact();
        contact.setContactId(contactId);
        contact.setFirstName(firstName);
        contact.setLastName(lastName);
        contact.setEmail(email);
        try{
        entityManager.getTransaction().begin();
        
        entityManager.persist(contact);
        entityManager.getTransaction().commit();
        }catch(Throwable t){
          if(entityManager.getTransaction().isActive())
            entityManager.getTransaction().rollback();
          contact = null;
        }finally{
          entityManager.close();
        }
        logger.debug("Exiting ContactService.insertContact()");
        return contact;
      }
    
    
      @PUT
      @Path("/{contactId}")
      @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
      @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
      public Contact updateContact(@PathParam("contactId") int contactId,
          @FormParam("firstName") String firstName,
          @FormParam("lastName") String lastName,
          @FormParam("email") String email) {
        logger.debug("Entering ContactService.update() contactId" + contactId);
    
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Contact contact = new Contact();
        contact.setContactId(contactId);
        contact.setFirstName(firstName);
        contact.setLastName(lastName);
        contact.setEmail(email);
        try{
        entityManager.getTransaction().begin();
        entityManager.merge(contact);
        entityManager.getTransaction().commit();
        }catch(Throwable t){
          if(entityManager.getTransaction().isActive())
            entityManager.getTransaction().rollback();
          contact = null;
        }finally{
          entityManager.close();
        }
        logger.debug("Exiting ContactService.updateContact()");
    
        return contact;
      }
    
      @DELETE
      @Path("/{contactId}")
    
      public void deleteContact(@PathParam("contactId") int contactId) {
        logger.debug("Entering ContactService.deleteContact() contactId " + contactId);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        try{
          entityManager.getTransaction().begin();
          Contact contact = entityManager.find(Contact.class, contactId);
          logger.debug("remove contact " + contact);
          entityManager.remove(contact);
          logger.debug("After removing " + contact);
          entityManager.getTransaction().commit();
          }catch(Throwable t){
            if(entityManager.getTransaction().isActive())
              entityManager.getTransaction().rollback();
    
          }finally{
            entityManager.close();
          }
        logger.debug("Exiting ContactService.deleteContact()");
      }
    
    }
    
    The ContactService class exposes CRUD operations on the CONTACT table using REST

1 comment:

Mohit Kumar said...

Excellent article..really helped me move forward in my current assignment.
Thanks.