What is Hibernate Mapping file?

In the previous article, we have seen What is persistence class in Hibernate. You can check it here What is Persistent classes in Hibernate?. In this article, we will cover What is mapping file in Hibernate?

As we know, the responsibility of Hibernate is to map the Persistence class to the database. But How Hibernate will come to know about it? So for this, we have one mapping file in Hibernate that we will discuss here.

What is Mapping File?

Mapping is the most important part of the Hibernate Framework.

  • The ORM(Object Relational Mapping) that does the working of mapping of persistence classes to the database needs a mapping file.
  • We create a mapping file in the XML document.
  • We can also do mapping using annotation from JDK 1.5 version.

How to create a mapping file?

While creating a mapping file just right-click on the project name select File then save it as <classname>.hbm.xml. We will see an example of mapping.

Create a Table

<-------create a table---------->
CREATE TABLE `job_table` (
  `id` int NOT NULL,
  `job_type` varchar(45) DEFAULT NULL,
  `job_location` varchar(45) DEFAULT NULL,
  `doj` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) 
<------------------------------>

Create a Persistence Class

import java.util.Date;

public class Job {
  private int id;
  private String jobType;
  private String jobLocation;
  private Date doj;

  public int getId() {
    return id;
  }

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

  public String getJobType() {
    return jobType;
  }

  public void setJobType(String jobType) {
    this.jobType = jobType;
  }

  public String getJobLocation() {
    return jobLocation;
  }

  public void setJobLocation(String jobLocation) {
    this.jobLocation = jobLocation;
  }

  public Date getDoj() {
    return doj;
  }

  public void setDoj(Date doj) {
    this.doj = doj;
  }

  public Job(int id, String jobType, String jobLocation, Date doj) {
    super();
    this.id = id;
    this.jobType = jobType;
    this.jobLocation = jobLocation;
    this.doj = doj;
  }
  public Job() {
    super();
    // TODO Auto-generated constructor stub
  }
}

Create a Mapping file job.hbm.xml file

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Job" table="job_table">
    <id name="id">
      <generator class="assigned"></generator>
    </id>
    <property name="jobType" column="job_type"></property>
    <property name="jobLocation" column="job_location"></property>
    <property name="doj" column="doj"></property>
  </class>

</hibernate-mapping>

Let’s understand each element in Mapping File

  • The starting of the mapping file contains the DTD for the file i.e Document Type Definition for the XML file.
  • The root element is <hibernate-mapping> element which shows the starting point.
  • To map the Java class or the persistent class to the database we need to provide it in the <class> element. 
  • The <class element> contains two attributes – name and table name in order to know which class to map to which table.
  • Next, we have the <id> element which is for the primary key that will uniquely identify the data. it has attribute name, type, and column.
  • Now, How we should get the id that we can define in <generater> element class which has some classes that we will discuss in this article.
  • The mapping of attributes from classes to table is done using the <property> element. It also has a name, column, and type attribute.

What is Generator Class in Mapping file?

It is an important part of the Hibernate Mapping file. It is used to generate an id. There are several classes available in the <generator> element.

  • assigned: It is the default generator for the id. If we don’t write any generator class it will default consider it assigned. It is associated with the Assigned class.
<id name="id" column="id">
<generator class="assigned"/>
</id>
  • increment: This generator class increments the id value by 1. It is associated with the IncreamentGenerator class.
<id name="id" column="id">
<generator class="increment"/>
</id>
  • sequence: This generator class uses the sequence for the database. It is a SequenceGenerator class.
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">NewSequence</param>
</id>
  • hilo: This class follows the High and low algorithm for generating id. It is associated with TableHiloGenerator class.
<id name="id" column="id">
<generator class="hilo">
<param name="table">table1</param>
<param name="column">col1</param>
<param name="max_lo">20</param>
</id>
  • identity: It also generates id. The database used here will be Sybase, HypersonicSQL, MS SQL, Sybase and etc. It is associated with IdentityGenerator class.
<id name="id" column="id">  
  <generator class="identity"/> 
 </id>
  • native: It depends on the database. It uses all identity, sequence, and Hilo. 
<id name="id" column="id">  
  <generator class="native"/>  
 </id>
  • foreign: This class is used for the foreign key in the table. It is associated with ForeignGenerator class.
<id name="id" column="id">
  <generator class="foreign" />
</id>
  • uuid: This class generates the alphanumeric primary key.
<id name="id" column="id">  
  <generator class="uuid"/>  
 </id>
  • guid: This class is available for some databases like MS SQL and MYSQL databases.
<id name="id" column="id">  
  <generator class="guid"/>  
 </id>

Thus this is How we create a Mapping file in Hibernate and also discussed Different Generator classes.

In the next article of this article, we will see some How mapping is done for Collection and association Mapping with a simple example.