Hibernate Mapping: How properties of a class are mapped to the columns of a database table in Hibernate?
Hibernate 5 is very powerful yet flexible ORM framework. In this page we will see how Hibernate is mapping Java class to database table.
There are two ways you can map Java Object to database table:
a) Using the XML file
b) Using annotations
a) Using the XML file to map Java properties to database table
Hibernate 5 is mostly using the annotations for mapping Java class and its variables to database table and respective columns. But it also supports XML based mapping but this method is less popular.
Following is an example of XML mapping. The mapping file used is Employee.hbm.xml and it maps net.roseindia.table.Employee to database table employee. Here is the code of the xml mapping file.
<?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="net.roseindia.table.Employee" table="employee"> <id name="empId" type="int" column="emp_id"> <generator class="native" /> </id> <property name="EmpName" type="string" column="emp_name" /> <property name="salary" type="int" column="emp_salary" /> <property name="designation" type="string" column="designation" /> <property name="address" type="string" column="address" /> </class> </hibernate-mapping>
Check complete example at Hibernate hbm.xml.
b) Using annotations to mapping
The use annotations is the preferred way of mapping Java POJO class with the database table and its fields.
It uses annotated POJO class for mapping.
Following annotations are most frequently used for mapping:
@Entity - Define a class as an Entity
@Table - Table name to for mapping this entity
@Id - Primary key mapping and it maps the annotated field as primary key
@GeneratedValue - Primay Key generated from database
@Column - Mapping Java variable to table column
@ManyToMany - Many to many relationship mapping
@JoinTable - Joining entity
@JoinColumn - Table relationship mapping
Here is sample example mapping:
package net.roseindia.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * @author Deepak Kumar * Web: http://www.roseindia.net */ @Entity @Table(name = "employee") public class Employee implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private int id; @Column(name="emp_name") private String empName; @Column(name="emp_address") private String empAddress; @Column(name="emp_mobile_nos") private String empMobileNos; .....
In this above example we have used @Entity, @Table, @Id, @Column etc.. annotations. You can the the tutorial Hibernate 5 Annotation Example for complete source code with running example.
We have 1000s of Hibernate ORM tutorials. Here are Hibernate 5 tutorial links: