Hibernate O/R Mapping

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Hibernate is a pure Java object-relational mapping and persistence framework that allows you to map plain old Java objects to relational database tables using Using Annotation for Model Class Object Mapping and Another way Using Hibernate O/R Mapping file(.hbm.xml) for Model Class Object Mapping. Using Hibernate can save a lot of development time on a project, since the whole JDBC layer is managed by the framework. Hibernate is a mediator that connects the object-oriented environment to the relational environment. It provides persistence services for an application by performing all of the required operations in the communication between the object-oriented and relational environments. Storing, updating, removing, and loading can be done. Hibernate have a number of advantages over other similar object-relational mapping approaches (JDO, entity beans, in-house development, and so on): it's free and open source, it has achieved a good level of maturity, it`s widely used, and it has a very active community forum.

To integrate Hibernate into an existing Java project, you will need to follow below steps:

  • Download the latest release of the Hibernate framework from the Hibernate Web site.
  • Copy the necessary Hibernate libraries (JAR files) to your application`s CLASSPATH.
  • Create the XML configuration files that will be used to map your Java objects to your database tables.
  • Copy the XML configuration files to your application`s CLASSPATH.

Using Java Annotations

Following is the mapping of Employee class with annotations to map objects with the EMPLOYEE table

import javax.persistence.*;

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
   @Id @GeneratedValue
   @Column(name = "id")
   private int id;

   @Column(name = "first_name")
   private String firstName;

   @Column(name = "last_name")
   private String lastName;

   
   public Employee() {}
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
}

Following point will explain the annotations used in the above class.

  • @Entity annotation is used to mark an object as an entity.
  • @Table annotation lets you define the table the entity will be persisted into. You can also optionally define the catalog, the schema as well as unique constraints on the table.
  • @Id annotation lets you define the primary key column of database table.
  • @Column annotation is used to specify the details of the column to which a field or property will be mapped.

Using Mapping file (.hbm.xml)

<?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="com.javatutsworld.dto.Employee" table=" EMPLOYEE">
   <id name="id" type="long" column="id">
       <generator class="assigned"/>
   </id>

   <property name="firstName"><column name="first_name" /></property>
   <property name="lastName"><column name="last_name" /></property>
</class>
</hibernate-mapping>

Following point will explain the mapping used in the above class.

<hibernate-mapping> element: The first or root element of hibernate mapping document is <hibernate-mapping> element. Between the <hibernate-mapping> tag class element(s) are present.

<class> element: The <Class> element maps the class object with corresponding entity in the database. It also tells what table in the database has to access and what column in that table it should use. Within one <hibernate-mapping> element, several <class> mappings are possible.

<id> element: The <id> element in unique identifier to identify an object. <id> element map with the primary key of the table.

<generator> element: Used to create primary key for new record, there are some commonly used generators type given below.

  • Increment: used to generate primary keys of type long, short or int that are unique only.
  • Sequence: used to generate primary keys for DB2, Oracle, SAP Database.
  • Assigned: used when application code generates the primary key.
  • Native: selects identity, sequence or hilo depending upon the capabilities of the underlying db.
  • Identity: supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
  • Uuid: Unique use ID of 128 bits generated from using algorithm and return type is String
  • hilo: generated by the hi/lo Algorithm
  • seqhilo: generated by the hi/lo Algorithm according to sequence of database

<property> element: define standard Java attributes and their mapping into database.

      Advertisements