# Hibernate Configuration

In 
JPA
Published 2022-12-03

This tutorial will explain to you how to configure Hibernate (using Eclipse) for implementing JPA (Java Persistence API) in your Java application.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database. JPA is now considered the standard industry approach for Object to Relational Mapping (ORM) in the Java Industry. JPA itself is just a specification, not a product; it cannot perform persistence or anything else by itself. JPA is just a set of interfaces and requires an implementation. Here are some of the most known JPA implementations: Hibernate, Eclipselink, Toplink, Spring Data JPA.

In this tutorial you will see how to configure a Maven Java project for using JPA implemented by Hibernate.

If you have a Maven project you have to see pom.xml file like in the following picture:

Add the following dependency into the pom.xml file:

 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
 <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.10.Final</version>
</dependency>

Your pom.xml file must be like this:

I will configure Hibernate to connect to a 12c Oracle database. For doing this I will add the OJDBC driver for that database:

Now you can build the application with Maven and the Goal must be "install". Maven will download all the library it needs:

Create and add the file named hibernate.cfg.xml into a directory ("resources", for instance) and add it to the CLASSPATH. The content of hibernate.cfg.xml must be something like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

  <session-factory>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@pc:1521:db12c</property>
    <property name="hibernate.connection.username">SCOTT</property>
    <property name="hibernate.connection.password">s</property>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.default_schema">SCOTT</property>
    <property name="show_sql">true</property>
    
    
    <mapping resource="employee.hbm.xml"></mapping>
  </session-factory>

</hibernate-configuration>

As you can see, in "hibernate.cfg.xml" file you have all you need for establishing a connection to a database. In my case it is a 12c Oracle database. "employee.hbm.xml" file define the relationship between the database table and the Java object.

In my case, here it is its content:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="jpa_package.Employee" table="EMP">

     <id name="id" type="int">
          <column name="ID" precision="10" scale="0" />
          <generator class="assigned" />
     </id>

     <property name="name" type="string">
          <column name="NAME" length="20" not-null="true" />
     </property>

     <property name="job" type="string">
          <column name="JOB" length="20" not-null="true" />
     </property>

   </class>
</hibernate-mapping>

In this example, Employee is the Java class, created in jpa_package and EMP is the Oracle database table.

When all is done, you can consider your application ready to work with Hibernate (we can make database operation on well known SCOTT.EMP table).