Hibernate and JPA Example

In this example we are going to use Hibernate and JPA to create a simple program that inserts data into database. We have used Hibernate 5 and JPA 2.1 for this program.

Hibernate and JPA Example

Hibernate and JPA Example program for saving data

In this tutorial we are integrating Hibernate with JPA and writing simple program to insert data into MySQL database table. Hibernate 5 is the latest version of Hibernate ORM framework and we are using it with JPA 2.1 in the program. We have used Eclipse for easy compilation and execution of program. Maven is used to satisfy the Hibernate and JPA dependency in the project.

Hibernate ORM is evolved much from  its initial version 1.0 and its now one of the most used ORM framework in enterprise application development world. Java Persistence API or JPA for short is the Specification from Oracle Java for writing database applications. In JPA Hibernate is used as one of the persistence provider. There other open source and commercial persistence provider in the market, but Hibernate is open-source free ORM tool which is used widely in the programming world.

Through this example we are teaching you to configure and use Hibernate 5 ORM with JPA based enterprise applications. You can extend this program to try CRUD operations using JPA API.

You can download the source code of this project and easily import in Eclipse IDE.  After importing project in Eclipse you will be able to quickly run and see the application. This way you can learn Hibernate and JPA integration quickly.

I am assuming that you have prior experience with with Hibernate and JPA based application development.

Hibernate and JPA Example

So, let's start developing the program.

Step 1: Create database and table

We are using MySQL database and there we will create table. Here is the query to create database:

create database jpa;

Now create the table using following SQL:

CREATE TABLE `products` (                                
            `id` int(11) NOT NULL AUTO_INCREMENT,                  
            `product_name` varchar(250) DEFAULT NULL,              
            `product_description` varchar(400) DEFAULT NULL,       
            `stock_qty` double(10,2) DEFAULT NULL,                 
            `price` double(10,2) DEFAULT NULL,                     
            PRIMARY KEY (`id`)                                     
   ) ENGINE=InnoDB;

Step 2: Create the entity class

Entity class is simple POJO class with annotation which maps to the database table. JPA uses this class and persist the data from this class into database while saving object in database. If you search data from database, JPA will load data in the object of this(Entity) class and return to the calling program. Here is the code of the Entity class:

package net.roseindia.model;

import javax.persistence.*;

/**
* @author Deepak Kumar
* More tutorials at http://www.roseindia.net
*/

@Entity
@Table(name="products")
public class Product {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	
	@Column(name="product_name")
	private String productName;
	
	@Column(name="product_description")
	private String productDescription;
	
	@Column(name="stock_qty")
	private Double stockQty;
	
	@Column(name="price")
	private Double price;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductDescription() {
		return productDescription;
	}

	public void setProductDescription(String productDescription) {
		this.productDescription = productDescription;
	}

	public Double getStockQty() {
		return stockQty;
	}

	public void setStockQty(Double stockQty) {
		this.stockQty = stockQty;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}
	

}

You can view the details of the annotations used here at http://www.roseindia.net/jee/jee7/jpa/first-jpa-example.shtml

Step 3: Create persistence.xml file

JPA framework uses this class to instantiate runtime for integrating with the database. We will use this persistence.xml file and initialize JPA runtime in our program. Here is the example of persistence.xml file.

<?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://java.sun.com/xml/ns/persistence                  
                                http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="psunit1">
 	
		<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
		<class>net.roseindia.model.Product</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.url" 
			value="jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            
        </properties>
	
  </persistence-unit>
</persistence>

In the above file you will find the org.hibernate.jpa.HibernatePersistenceProvider, which is the persistence provide in of Hibernate. In the above file we have other properties like database url, username and password. You can change these values to map to your database.

Step 4: Add Hibernate and JPA Dependencies in pom.xml file

Add maven dependency of Hibernate 5 and JPA in pom.xml file. Here is full code of pom.xml file used in project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--
* @author Deepak Kumar
* More tutorials at http://www.roseindia.net
* -->
   <modelVersion>4.0.0</modelVersion>
    <groupId>net.roseindia</groupId>
    <artifactId>JPA2.1examples</artifactId>
    <version>7.0</version>
    <packaging>war</packaging>
    <dependencies>
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-api</artifactId>
			<version>7.0</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		 <dependency> 
			<groupId>javax.json</groupId> 
			<artifactId>javax.json-api</artifactId> 
			<version>1.0</version> 
		</dependency> 
		<dependency>
		  <groupId>org.glassfish</groupId>
		  <artifactId>javax.json</artifactId>
		  <version>1.0</version>
		  <scope>runtime</scope>
		</dependency>
	
	    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
    </dependency>
	
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-entitymanager</artifactId>
		<version>5.2.12.Final</version>
	</dependency>


    <!-- jdbc driver for MySQL -->
	
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.6</version>
		<scope>runtime</scope>
	</dependency>
         
		 <!-- optional -->

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-osgi</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-proxool</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-infinispan</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>5.2.12.Final</version>
</dependency>
			
	
    </dependencies>


    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

</project>

From the above pom.xml file you can note that we have added Hibernate 5 dependency version: 5.2.12.Final. We are using JPA 2.1.0 in this example. Maven tool will download all the libraries files from remote repositories in your project. Now the next and final step is to run the program and see data in database.

Step 5: Code for executing the example

Here is the code for executing the example program:

package net.roseindia;

import net.roseindia.model.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.ParameterMode;
import javax.persistence.Persistence;
import javax.persistence.StoredProcedureQuery;
		/*
		//Compile mvn compile
		//Run: mvn exec:java -Dexec.mainClass="net.roseindia.AppTest"
		
		*/
/**
* @author Deepak Kumar
* More tutorials at http://www.roseindia.net
*/
		
public class AppTest {

	private static final String PERSISTENCE_UNIT_NAME = "psunit1";
	private static EntityManagerFactory factory;

	public static void main(String[] args) {
		factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		EntityManager em = factory.createEntityManager();

		em.getTransaction().begin();
		
		Product product = new Product();
		product.setProductName("JPA 2.1 Book");
		product.setProductDescription("This is the latest book on JPA 2.1");
		product.setStockQty(100.00);
		product.setPrice(95.99);
		em.persist(product);
		em.getTransaction().commit();
		em.close();
		
	}
}

The EntityManager API provides functions for interacting with the database such as performing save operation. For saving an object we have to call the persist() function.

Use the following command for compiling the code:

mvn compile

Here is the output of the compilation:

C:\Users\user>g:

G:\>cd G:\fistexample

G:\fistexample>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building JPA2.1examples 7.0
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/javax/json/javax.json-api/1.0/javax.json-api-1.0.pom
Downloaded: http://repo.maven.apache.org/maven2/javax/json/javax.json-api/1.0/javax.json-api-1.0.pom (7 KB at 1.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/eclipse/persistence/javax.persistence/2.1.0/javax.persistence-2.1.0.pom
Downloaded: http://repo.maven.apache.org/maven2/org/eclipse/persistence/javax.persistence/2.1.0/javax.persistence-2.1.0.pom (3 KB at 6.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-entitymanager/5.2.12.Final/hibernate-entitymanager-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-entitymanager/5.2.12.Final/hibernate-entitymanager-5.2.12.Final.pom (4 KB at 8.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.pom (6 KB at 15.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-core/5.2.12.Final/hibernate-core-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-core/5.2.12.Final/hibernate-core-5.2.12.Final.pom (4 KB at 9.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.pom
Downloaded: http://repo.maven.apache.org/maven2/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.pom (10 KB at 24.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.1.Final/jboss-transaction-api_1.2_spec-1.0.1.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.1.Final/jboss-transaction-api_1.2_spec-1.0.1.Final.pom (5 KB at 12.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/20/jboss-parent-20.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/20/jboss-parent-20.pom (34 KB at 57.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/jandex/2.0.3.Final/jandex-2.0.3.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/jandex/2.0.3.Final/jandex-2.0.3.Final.pom (6 KB at 14.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/12/jboss-parent-12.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/12/jboss-parent-12.pom (31 KB at 73.4 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/fasterxml/classmate/1.3.0/classmate-1.3.0.pom
Downloaded: http://repo.maven.apache.org/maven2/com/fasterxml/classmate/1.3.0/classmate-1.3.0.pom (6 KB at 14.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/fasterxml/oss-parent/24/oss-parent-24.pom
Downloaded: http://repo.maven.apache.org/maven2/com/fasterxml/oss-parent/24/oss-parent-24.pom (19 KB at 48.4 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/common/hibernate-commons-annotations/5.0.1.Final/hibernate-commons-annotations-5.0.1.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/common/hibernate-commons-annotations/5.0.1.Final/hibernate-commons-annotations-5.0.1.Final.pom (2 KB at 4.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy/1.6.14/byte-buddy-1.6.14.pom
Downloaded: http://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy/1.6.14/byte-buddy-1.6.14.pom (12 KB at 27.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy-parent/1.6.14/byte-buddy-parent-1.6.14.pom
Downloaded: http://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy-parent/1.6.14/byte-buddy-parent-1.6.14.pom (26 KB at 61.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-osgi/5.2.12.Final/hibernate-osgi-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-osgi/5.2.12.Final/hibernate-osgi-5.2.12.Final.pom (3 KB at 7.1 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/javax/interceptor/javax.interceptor-api/1.2/javax.interceptor-api-1.2.pom
Downloaded: http://repo.maven.apache.org/maven2/javax/interceptor/javax.interceptor-api/1.2/javax.interceptor-api-1.2.pom (14 KB at 29.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom
Downloaded: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.pom (2 KB at 3.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.compendium/4.3.1/org.osgi.compendium-4.3.1.pom
Downloaded: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.compendium/4.3.1/org.osgi.compendium-4.3.1.pom (2 KB at 3.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-envers/5.2.12.Final/hibernate-envers-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-envers/5.2.12.Final/hibernate-envers-5.2.12.Final.pom (2 KB at 4.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-c3p0/5.2.12.Final/hibernate-c3p0-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-c3p0/5.2.12.Final/hibernate-c3p0-5.2.12.Final.pom (3 KB at 5.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/mchange/c3p0/0.9.5.2/c3p0-0.9.5.2.pom
Downloaded: http://repo.maven.apache.org/maven2/com/mchange/c3p0/0.9.5.2/c3p0-0.9.5.2.pom (2 KB at 3.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/mchange/mchange-commons-java/0.2.11/mchange-commons-java-0.2.11.pom
Downloaded: http://repo.maven.apache.org/maven2/com/mchange/mchange-commons-java/0.2.11/mchange-commons-java-0.2.11.pom (4 KB at 8.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-proxool/5.2.12.Final/hibernate-proxool-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-proxool/5.2.12.Final/hibernate-proxool-5.2.12.Final.pom (3 KB at 4.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/proxool/proxool/0.8.3/proxool-0.8.3.pom
Downloaded: http://repo.maven.apache.org/maven2/proxool/proxool/0.8.3/proxool-0.8.3.pom (148 B at 0.4 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-infinispan/5.2.12.Final/hibernate-infinispan-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-infinispan/5.2.12.Final/hibernate-infinispan-5.2.12.Final.pom (3 KB at 5.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-core/8.2.5.Final/infinispan-core-8.2.5.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-core/8.2.5.Final/infinispan-core-8.2.5.Final.pom (8 KB at 19.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-parent/8.2.5.Final/infinispan-parent-8.2.5.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-parent/8.2.5.Final/infinispan-parent-8.2.5.Final.pom (86 KB at 144.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-bom/8.2.5.Final/infinispan-bom-8.2.5.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-bom/8.2.5.Final/infinispan-bom-8.2.5.Final.pom (30 KB at 74.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/arquillian/arquillian-bom/1.1.5.Final/arquillian-bom-1.1.5.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/arquillian/arquillian-bom/1.1.5.Final/arquillian-bom-1.1.5.Final.pom (11 KB at 26.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-bom/2.1.0/shrinkwrap-resolver-bom-2.1.0.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-bom/2.1.0/shrinkwrap-resolver-bom-2.1.0.pom (5 KB at 12.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/descriptors/shrinkwrap-descriptors-bom/2.0.0-alpha-5/shrinkwrap-descriptors-bom-2.0.0-alpha-5.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/descriptors/shrinkwrap-descriptors-bom/2.0.0-alpha-5/shrinkwrap-descriptors-bom-2.0.0-alpha-5.pom (5 KB at 12.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/wildfly/core/wildfly-core-parent/2.0.10.Final/wildfly-core-parent-2.0.10.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/wildfly/core/wildfly-core-parent/2.0.10.Final/wildfly-core-parent-2.0.10.Final.pom (69 KB at 143.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/19/jboss-parent-19.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/19/jboss-parent-19.pom (0 B at 0.0 KB/sec)
Downloading: https://repository.jboss.org/nexus/content/groups/public/org/jboss/jboss-parent/19/jboss-parent-19.pom
Downloaded: https://repository.jboss.org/nexus/content/groups/public/org/jboss/jboss-parent/19/jboss-parent-19.pom (0 B at 0.0 KB/sec)
Downloading: http://repository.jboss.org/nexus/content/groups/public/org/jboss/jboss-parent/19/jboss-parent-19.pom
Downloaded: http://repository.jboss.org/nexus/content/groups/public/org/jboss/jboss-parent/19/jboss-parent-19.pom (0 B at 0.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-commons/8.2.5.Final/infinispan-commons-8.2.5.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-commons/8.2.5.Final/infinispan-commons-8.2.5.Final.pom (6 KB at 13.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jgroups/jgroups/3.6.7.Final/jgroups-3.6.7.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jgroups/jgroups/3.6.7.Final/jgroups-3.6.7.Final.pom (20 KB at 49.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/marshalling/jboss-marshalling-osgi/1.4.10.Final/jboss-marshalling-osgi-1.4.10.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/marshalling/jboss-marshalling-osgi/1.4.10.Final/jboss-marshalling-osgi-1.4.10.Final.pom (3 KB at 4.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/marshalling/jboss-marshalling-parent/1.4.10.Final/jboss-marshalling-parent-1.4.10.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/marshalling/jboss-marshalling-parent/1.4.10.Final/jboss-marshalling-parent-1.4.10.Final.pom (7 KB at 16.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/11/jboss-parent-11.pom
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/jboss-parent/11/jboss-parent-11.pom (31 KB at 74.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-ehcache/5.2.12.Final/hibernate-ehcache-5.2.12.Final.pom
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-ehcache/5.2.12.Final/hibernate-ehcache-5.2.12.Final.pom (3 KB at 5.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache/2.10.3/ehcache-2.10.3.pom
Downloaded: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache/2.10.3/ehcache-2.10.3.pom (19 KB at 48.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache-root/2.10.3/ehcache-root-2.10.3.pom
Downloaded: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache-root/2.10.3/ehcache-root-2.10.3.pom (17 KB at 43.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache-parent/2.18/ehcache-parent-2.18.pom
Downloaded: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache-parent/2.18/ehcache-parent-2.18.pom (17 KB at 41.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.pom
Downloaded: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.pom (0 B at 0.0 KB/sec)
Downloading: http://www.terracotta.org/download/reflector/snapshots/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.pom
Downloading: http://www.terracotta.org/download/reflector/releases/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.pom
Downloading: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-parent/1.7.7/slf4j-parent-1.7.7.pom
Downloaded: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-parent/1.7.7/slf4j-parent-1.7.7.pom (0 B at 0.0 KB/sec)
Downloading: http://www.terracotta.org/download/reflector/snapshots/org/slf4j/slf4j-parent/1.7.7/slf4j-parent-1.7.7.pom
Downloading: http://www.terracotta.org/download/reflector/releases/org/slf4j/slf4j-parent/1.7.7/slf4j-parent-1.7.7.pom
Downloading: http://repo.maven.apache.org/maven2/javax/json/javax.json-api/1.0/javax.json-api-1.0.jar
Downloading: http://repo.maven.apache.org/maven2/org/eclipse/persistence/javax.persistence/2.1.0/javax.persistence-2.1.0.jar
Downloading: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-entitymanager/5.2.12.Final/hibernate-entitymanager-5.2.12.Final.jar
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-core/5.2.12.Final/hibernate-core-5.2.12.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/javax/json/javax.json-api/1.0/javax.json-api-1.0.jar (20 KB at 47.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/jandex/2.0.3.Final/jandex-2.0.3.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/jandex/2.0.3.Final/jandex-2.0.3.Final.jar (183 KB at 311.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/fasterxml/classmate/1.3.0/classmate-1.3.0.jar
Downloaded: http://repo.maven.apache.org/maven2/com/fasterxml/classmate/1.3.0/classmate-1.3.0.jar (63 KB at 150.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/common/hibernate-commons-annotations/5.0.1.Final/hibernate-commons-annotations-5.0.1.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/common/hibernate-commons-annotations/5.0.1.Final/hibernate-commons-annotations-5.0.1.Final.jar (74 KB at 167.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-entitymanager/5.2.12.Final/hibernate-entitymanager-5.2.12.Final.jar (591 B at 0.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar (0 B at 0.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy/1.6.14/byte-buddy-1.6.14.jar
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar (66 KB at 25.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.1.Final/jboss-transaction-api_1.2_spec-1.0.1.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/eclipse/persistence/javax.persistence/2.1.0/javax.persistence-2.1.0.jar (159 KB at 51.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-osgi/5.2.12.Final/hibernate-osgi-5.2.12.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.1.Final/jboss-transaction-api_1.2_spec-1.0.1.Final.jar (28 KB at 38.5 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/javax/interceptor/javax.interceptor-api/1.2/javax.interceptor-api-1.2.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-osgi/5.2.12.Final/hibernate-osgi-5.2.12.Final.jar (22 KB at 22.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar
Downloaded: http://repo.maven.apache.org/maven2/javax/interceptor/javax.interceptor-api/1.2/javax.interceptor-api-1.2.jar (24 KB at 27.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.compendium/4.3.1/org.osgi.compendium-4.3.1.jar
Downloaded: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar (342 KB at 136.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-envers/5.2.12.Final/hibernate-envers-5.2.12.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy/1.6.14/byte-buddy-1.6.14.jar (2731 KB at 496.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-c3p0/5.2.12.Final/hibernate-c3p0-5.2.12.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-c3p0/5.2.12.Final/hibernate-c3p0-5.2.12.Final.jar (12 KB at 16.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/mchange/c3p0/0.9.5.2/c3p0-0.9.5.2.jar
Downloaded: http://repo.maven.apache.org/maven2/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.jar (733 KB at 111.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/mchange/mchange-commons-java/0.2.11/mchange-commons-java-0.2.11.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-envers/5.2.12.Final/hibernate-envers-5.2.12.Final.jar (462 KB at 204.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-proxool/5.2.12.Final/hibernate-proxool-5.2.12.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/osgi/org.osgi.compendium/4.3.1/org.osgi.compendium-4.3.1.jar (750 KB at 147.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/proxool/proxool/0.8.3/proxool-0.8.3.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-proxool/5.2.12.Final/hibernate-proxool-5.2.12.Final.jar (12 KB at 17.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-infinispan/5.2.12.Final/hibernate-infinispan-5.2.12.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/com/mchange/c3p0/0.9.5.2/c3p0-0.9.5.2.jar (487 KB at 325.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-core/8.2.5.Final/infinispan-core-8.2.5.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-infinispan/5.2.12.Final/hibernate-infinispan-5.2.12.Final.jar (167 KB at 124.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-commons/8.2.5.Final/infinispan-commons-8.2.5.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/proxool/proxool/0.8.3/proxool-0.8.3.jar (465 KB at 211.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jgroups/jgroups/3.6.7.Final/jgroups-3.6.7.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/com/mchange/mchange-commons-java/0.2.11/mchange-commons-java-0.2.11.jar (593 KB at 103.4 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/jboss/marshalling/jboss-marshalling-osgi/1.4.10.Final/jboss-marshalling-osgi-1.4.10.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-commons/8.2.5.Final/infinispan-commons-8.2.5.Final.jar (679 KB at 162.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-ehcache/5.2.12.Final/hibernate-ehcache-5.2.12.Final.jar
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-ehcache/5.2.12.Final/hibernate-ehcache-5.2.12.Final.jar (139 KB at 126.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache/2.10.3/ehcache-2.10.3.jar
Downloaded: http://repo.maven.apache.org/maven2/org/jboss/marshalling/jboss-marshalling-osgi/1.4.10.Final/jboss-marshalling-osgi-1.4.10.Final.jar (368 KB at 108.9 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar
Downloaded: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar (0 B at 0.0 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/org/jgroups/jgroups/3.6.7.Final/jgroups-3.6.7.Final.jar (2341 KB at 252.4 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/org/infinispan/infinispan-core/8.2.5.Final/infinispan-core-8.2.5.Final.jar (3425 KB at 282.6 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/org/hibernate/hibernate-core/5.2.12.Final/hibernate-core-5.2.12.Final.jar (6525 KB at 260.1 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/net/sf/ehcache/ehcache/2.10.3/ehcache-2.10.3.jar (8702 KB at 349.7 KB/sec)
Downloading: http://www.terracotta.org/download/reflector/snapshots/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar
Downloading: http://www.terracotta.org/download/reflector/releases/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ JPA2.1examples ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ JPA2.1examples ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:42.353s
[INFO] Finished at: Sun Jan 21 12:34:52 IST 2018
[INFO] Final Memory: 10M/157M
[INFO] ------------------------------------------------------------------------
G:\fistexample>

After compilation is done you can proceed to execute example program. Following the following instruction to run your Hibernate JPA integrated example code.

Use the following command for executing the code:

mvn exec:java -Dexec.mainClass="net.roseindia.AppTest" 

Alternatively you can run it from Eclipse IDE.

Download the source code of the project discussed here. 

Read Complete Hibernate 5 Tutorials.