Introduction of Mapping in Hibernate with Example by R4R Team

In Hibernate we have to map xml file to with the help of database. Hibernate provide how to map the defined class or classes to the database tables. So for this we need to first create a database than we need to create a table with valid datatype which we want to use in the hibernate and in the jsp page to get the value and store in the database. When we have created the database then we can map to database with the help of xml file in hibernate.

We can create xml file by own or we can take the help from which IDE we are using. Or we can inclued by XDoclet, Middlegen and AndroMDA to generate the xml file. It is for the advance type of user.

First we neet to create the table in the MySql DataBase.

create table WORKER (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

Now Here we take a POJO (Plain old Java object) for the configuration mapping with the database.

public class HibernateConfiguration {
   private int id;
   private String firstName; 
   private String lastName;   
   private int salary;  

   public HibernateConfiguration() {}
   public HibernateConfiguration(String fname, String lname, int salary) {
      this.firstName = fname;
      this.lastName = lname;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName( String first_name ) {
      this.firstName = first_name;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName( String last_name ) {
      this.lastName = last_name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}

All above given two entities we can define following mapping file which tells How hibernate map to defined class to the database table.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="HibernateConfiguration" table="WORKER">
      <meta attribute="class-description">
         This class contains the WORKER detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>

When we worte these line in the editor then we need to save this by <classname>.hbm.xml and we save to our mapping document in the file Worker.hbm.xml.

Mapping elements used in the mapping file those are below:

<hibernate-mapping>

The mapping document is an XML document having <hibernate-mapping> as the root element which contains all the <class> elements.

<class>

The <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.

<meta>

The <meta> element is optional element and can be used to create the class description.

<id>

The <id> element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

<generator>

The <generator> element within the id element is used to automatically generate the primary key values. Set the class attribute of the generator element is set to native to let hibernate pick up either identity, sequence or hilo algorithm to create primary key depending upon the capabilities of the underlying database

<property>

The <property> element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!