Using Hibernate 5 on Payara Server

Photo of Mert Caliskan by Mert Caliskan

Hibernate is the object/relational mapping tool that handles mapping of Java classes to relational tables and Java types to SQL data type. It’s a well-known framework in the Enterprise Java eco-system since it’s being actively developed for the last 16 years.

With this article, I’m going to show the ways of using Hibernate inside a sample application – source code available here – and deploy it onto Payara Server. I will be using the latest version of Hibernate, which is 5.2.10.Final at the time of writing.


Getting Started

My sample project is a web application architected with Maven and it contains one simple domain object, its DAO implementation and its REST based web service for exposing CRUD operations to the outside world.


Let’s start with the Maven configuration of the project. Hibernate is added as a single dependency to the project since it transitively fetches the necessary dependencies underneath.




For demonstration purposes, the in-memory H2 database will be used for easy configuration. Its dependency definition is as follows:




The JDBC resource and Connection Pool definitions are defined in a glassfish-resources.xml file, which is located under the WEB-INF folder. This helps to isolate the definitions within our sample project, and mitigates needing to deal with the Payara Server Admin Console or asadmin commands to define the resources.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-// GlassFish Application Server 3.1 Resource Definitions//EN"
    <jdbc-resource pool-name="H2Pool"
    <jdbc-connection-pool name="H2Pool"
        <property name="user" value="sa"/>
        <property name="password" value=""/>
        <property name="url" value="jdbc:h2:mem:hibernateExample"/>


You can see that H2 database URL is defined as in-memory, and the JNDI name mapping that should be used by the project is jdbc/hibernate.


The persistence.xml file is used to register a persistence unit to the application at the application level. I am using the registered JDBC Resource that was defined in the glassfish-resources.xml file, and naming the persistence unit as payaraHibernate.


<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns=""
    <persistence-unit name="payaraHibernate" transaction-type="JTA">
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="" value="create" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>


By defining the persistence provider, the application will use Hibernate as the underlying ORM. The H2 database is also related to the hibernate.dialect property so that it allows Hibernate to generate H2 optimized SQLs.


The Person domain object class is defined as follows:


        @NamedQuery(name = "Person.findOne", query = "select p from Person p where = :id"),
        @NamedQuery(name = "Person.getAll", query = "select p from Person p")
public class Person {

    private long id;
    private String name;
    private String lastName;

    public Person() {

    public Person(String name, String lastName) { = name;
        this.lastName = lastName;

    // getter methods


The DAO implementation for the corresponding Person domain class is as follows:


public class PersonDao {

    private EntityManager entityManager;

    public List<Person> getAll() {
        return entityManager.createNamedQuery("Person.getAll", Person.class).getResultList();

    public Person find(Long id) {
        return entityManager.createNamedQuery("Person.findOne", Person.class).setParameter("id", id).getSingleResult();

    public void save(Person person) {

    public void update(Person person) {

    public void delete(Person person) {


The REST web service implementation is as follows:


public class PersonResource {

    private PersonDao personDao;

    public List<Person> all() {
        return personDao.getAll();

    public void save(Person person) {;

    public void update(Person person) {

    public void delete(@PathParam("id") Long id) {
        Person person = personDao.find(id);


After deploying the application onto Payara Server, I can easily invoke the REST web services for creating/updating/deleting person information. To invoke REST, I favored Postman , a GUI tool for API invocation. You can import given Postman project  by yourself (see JSON fileand invoke endpoints deployed on localhost:8080. The URL for the endpoint would be http://localhost:8080/hibernate-example/resources/person for a local deployment.


The screenshot below shows the GET request on the provided URL.