Hibernate O/R Mapping
In this tutorial you will learn about the Hibernate O/RM (Object/Relational Mapping).
O/RM itself can be defined as a software or product that represents and/or convert the data between the application (written in Object-Oriented Language) and the database. In other words we can say O/RM maps an object to the relational database table. Hibernate is also an O/RM and is available as open source project. To use Hibernate with Java you will be required to create a file with .hbm.xml extension into which the mapping information will be provided. This file contains the mapping of Object with the relational table that provides the information to the Hibernate at the time of persisting the data. ORM abstracts the application from the process related to database such as saving, updating, deleting of objects from the relational database table.
We will understand the above concept with the help of an example
Example :
Employee.java
package roseindia; public class Employee { private long empId; private String empName; public Employee() { } public long getEmpId() { return this.empId; } public void setEmpId(long empId) { this.empId = empId; } public String getEmpName() { return this.empName; } public void setEmpName(String empName) { this.empName = empName; } }
employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="roseindia.Employee" table="employee"> <id name="empId" type="long" column="Id" > <generator class="assigned"/> </id> <property name="empName"> <column name="Name" /> </property> </class> </hibernate-mapping>
The figure given below demonstrate you how ORM abstracts the application to the database related process and vice-versa :
The above figure demonstrates that an ORM abstracts the both in respect to database how an application tries to persist the data and in respect to an application that how objects are stored in database.