Hello Friends In Last chapters you learned about how to write our first hibernate application, there are two approach we have used for that—
  1. Using Annotation for Model Class Object Mapping 
  2. Another way Using Hibernate O/R Mapping file(.hbm.xml) for Model Class Object Mapping.
In the last example we created userDetails.hbm.xml to map UserDetails Object to the UserDetails table in the database. Now let’s understand the each component of the mapping file.
To recall here is the content of userDetails.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.sdnext.hibernate.tutorial.dto.UserDetails” table=”UserDetails”>
       <id name=”userId” type=”long” column=”ID” >
            <generator class=”assigned”/>
     </id>

    <property name=”userName”>
         <column name=”UserName” />
    </property>
    </class>
</hibernate-mapping>

1.<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.
2. <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. 
3.<id> element:

  The <id> element in unique identifier to identify an object. In fact <id> element map with the primary key of the table. In our code :
<id name=”userId” type=”long” column=”ID” >

primary key maps to the ID field of the table UserDetails. Following is the attributes of <id> element
  • name – Property name of the persistence model class
  • type – The java data type used for that property
  • column – Name of the column for that property of the persistence object
  • unsaved-value – This is the value used to determine if a class has been made persistent. If the value of the id attribute is null, then it means that this object has not been persisted. 
4.<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  – is 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
  • select – select from database triggered value
  • foreign – associated with the other model objects
5.<property> element: define standard Java attributes and their mapping into database schema.  
That is all about the mapping file for O/R mapping. I hope you was understand its all elements and its working in the hibernate.

Now in the Next Chapter we will discuss more about Saving Objects using Hibernate APIs

                            <<Previous Chapter 5<<    >>Next Chapter7>>