Wednesday, September 21, 2011

Annotation based Mapping Vs XML ( .hbm.xml) based Mapping in Hibernate


There are two main ways to perform ORM (Object to Relational Mapping) in hibernate. Those can be summarized as follows.

  1. Annotation based Mapping
  1. XML based Mapping(using hbm.xml file)


today we are going to perform ORM using both of those approaches for the following table using the following POJO class (called Contact.java).

CREATE TABLE CONTACTS
(
    id              INT PRIMARY KEY AUTO_INCREMENT,
    firstname    VARCHAR(30),
    lastname    VARCHAR(30),
    telephone   VARCHAR(15),
    email         VARCHAR(30),
    created     TIMESTAMP DEFAULT NOW()
);


POJO Class


package sample.hibernate.chathuranga.classes;


public class Contact {

private Integer id;


private String firstname;


private String lastname;


private String email;


private String telephone;

public String getEmail() {
return email;
}

public String getTelephone() {
return telephone;
}

public void setEmail(String email) {
this.email = email;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public String getFirstname() {
return firstname;
}

public String getLastname() {
return lastname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public Integer getId() {
return id;
}

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







XML based Mapping(using hbm.xml file)

here the Object to Relational Mapping is done inside a xml file which has hbm.xml extension. This file is placed inside the resources directory of the project. You can see the below sample Contacts.hbm.xml 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 package="sample.hibrernate.chathuranga.classes">

<class name="Contact" table="CONTACTS">

<id name="id" column="ID">
<generator class="native"/>
</id>

<property name="firstname" column="FIRSTNAME" />
<property name="lastname" column="LASTNAME" />
<property name="email" column="EMAIL" />
<property name="telephone" column="TELEPHONE" />

</class>

</hibernate-mapping> 


Annotation based Mapping

here the mapping will be done inside the data model (in the POJO classes) using the specific notations (known as annotations). The above POJO class with annotation based ORM mapping is as follows.


package sample.hibernate.chathuranga.classes;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "CONTACTS")
public class Contact {

@Id
@Column(name = "ID")
@GeneratedValue
private Integer id;

@Column(name = "FIRSTNAME")
private String firstname;

@Column(name = "LASTNAME")
private String lastname;

@Column(name = "EMAIL")
private String email;

@Column(name = "TELEPHONE")
private String telephone;

public String getEmail() {
return email;
}

public String getTelephone() {
return telephone;
}

public void setEmail(String email) {
this.email = email;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public String getFirstname() {
return firstname;
}

public String getLastname() {
return lastname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public Integer getId() {
return id;
}

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



Important:

it is required to add necessary lines to reflect your mapping option inside the hibernate.cfg.xml file. This file is placed inside the resources directory of the project.


--------------------------------------------------------------------------------------------------------
If you are using xml based mapping, then declare mapping resource as follows.

<mapping resource="sample/hibernate/chathuranga/classes/Contact.hbm.xml"/>

(here i have created a package called sample/hibernate/chathuranga/classes inside the resources directory and the Contact.hbm.xml file has been placed there)

--------------------------------------------------------------------------------------------------------
if you are using annotation based mapping, then declare the mapping class as follows.

<mapping class="sample.hibernate.chathuranga.classes.Contact"/>

No comments:

Post a Comment