Spring-Hibernate Maven project- Part 2

Hibernate Mapping

Here the two domain classes have one-to-many relationship. One Person can have many Addresses while one Adress can have only one person. So there is a one-to-many relationship between Person and Address.

In hibernate we have to create the mapping file for each domain class. The mapping file represents the database table which is mapped with the domain class. The table will be created according to things indicated inside mapping classes.

Mapping file for Person.java: - Person.hbm.xml
Mapping file for Address.java: - Address.hbm.xml

Mapping files are located inside src/main/resources. If you have configured Hibernate to be used inside Eclipse you can simply use it. Otherwise let's configure eclpse for hibrenate.

Hibernate-Eclipse Configuration 

Download Hibernate tools from here.
Extract the HibernateTools-3.X.zip file and put all the files inside the features folder into the features folder of the eclipse installation directory and put all the files inside the plugins folder into the plugins folder of the ecilpse installation directory. Restart the eclipse.

Right click src/main/resource.
Create New --Package
Create a new package called hibernate.
Then right click on hibernate package and click  New--Other
In the New wizard Select Hibernate Icon and expand it.
Select Hibernate XML Mapping file (hbm.xml)

Click Next.

Type Person.hbm.xml as the file name.

Click Next.
In the Hibernate XML mapping file window, click Browse. A window will be opened named Select a class to map.
In the text field type Person.java. And select the correct class from the comming list of classes.

Click Ok and Click Finish

Similarly create the Addrees.hbm.xml file mapped with Address.java class.

Now the project structure will be as follows.

Let's put the content to mapping files.

<hibernate-mapping package="org.test.testapp.domain">
<class name="Person" table="PERSON">
  <id column="PERSON_ID" name="PersonID" type="long">
   <generator class="identity"></generator>
  <property column="PERSON_NAME" name="Name" type="string">
  <property column="TEL_NO" name="TelNo" type="string">
  <set cascade="all" name="AddressList">
   <key column="PERSON_ID"></key>
   <one-to-many class="Address">

Let's see the each an ever tag.
class tag: defines which is the class being used and table being mapped for the class.
id tag: An attribute inside class which is going to be primary key of the table (PersonID) 
         name: attribute name of the class
         type: type of the table column, to match with the class attribute type (Long=long)
         column: column name of the resulting table
property tag All the attributes in the class expect lists and id attribute
          name: attribute name of the class
         type: type of the table column, to match with the class attribute type (String=string)
         column: column name of the resulting table
 list tag: The lists inside class
         name: Name of the List inside the class
         key tag:Defines the column name of this table which goes as the foriegn key to the Address class
        one-to-many tag: This describes the one-to-many relationship which creats the List inside class. (One Project has many Addresses)
                 class : One-to-many relationship defining class (Address for Person)

<hibernate-mapping package="org.test.testapp.domain">
 <class name="Address" table="ADDRESS">
  <id column="ADDRESS_ID" name="AddressID" type="long">
   <generator class="identity">
  <property column="STREET" name="Street" type="string">
  <property column="CITY" name="City" type="string">
  <property column="AREACODE" name="AreaCode" type="string">
  <many-to-one cascade="all" class="Person" name="NewPerson">
Respond to the Project class set tag with one-to-many tag, here we have to define the many-to-one tag
     name: name of the attribute of class which reffers the Person class
     class: Class with whome this class makes the relationship
This will be added to the ADDRESS table as column name which defined in Person mapping file.(key column="PERSON_ID")  



Isaac Weber said…
Nice bblog thanks for posting

Popular posts from this blog

XSLT - Modify Date and DateTime value

Integrate With Mutual Certificate Authentication based Service

Yield Price Sri Lanka - Android Application