Tech I Enjoy Logo

Custom Search




  Home >> Hibernate

In this Page we shall be covering a simple example on how to use component
tag for persisting object with composition of other objects with it. 
By composition I mean, if the parent object dies , then the contained child
object(s) will die too. 
Hibernate has a provision for achieving this by using a tag 'component' 
that can be used under class tag for configuring a class hierarchy as follows:

Visitor
 |------>VisitorDetails
             |------------>Address
             

Visitor.java
package com.techienjoy.hibernate.example;

public class Visitor {

    long id;
    VisitorDetails details;

    public VisitorDetails getDetails() {
        return details;
    }


    public long getId() {
        return id;
    }


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


    public void setDetails(VisitorDetails details) {
        this.details = details;
    }
}
VisitorDetails.java
package com.techienjoy.hibernate.example;

public class VisitorDetails {
    String name;
    Address address;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
}
VisitorDetails.java
package com.techienjoy.hibernate.example;

public class Address {

    String houseNumber;
    String addrLine1;
    String addrLine2;
    String phone;
    public String getHouseNumber() {
        return houseNumber;
    }
    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }
    public String getAddrLine1() {
        return addrLine1;
    }
    public void setAddrLine1(String addrLine1) {
        this.addrLine1 = addrLine1;
    }
    public String getAddrLine2() {
        return addrLine2;
    }
    public void setAddrLine2(String addrLine2) {
        this.addrLine2 = addrLine2;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}
Visitor.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 package="com.techienjoy.hibernate.example">
   <class name="Visitor" table="VISITOR">
    <id name="id" type="long" column="ID"/>
    <component name="details" class="VisitorDetails">
      <property name="name" column="NAME"/>
      <component name="address" class="Address">
        <property name="houseNumber" column="HOUSE_NUMBER" not-null="true"/>
        <property name="addrLine1" column="ADDRLINE1"/>
        <property name="addrLine2" column="ADDRLINE2"/>
        <property name="phone" column="PHONE"/>
      </component>
    </component>
   </class>
</hibernate-mapping>
Testing this example can be done using following Test Harness Code: Test.java
package com.techienjoy.hibernate.example;

/**
 * This code is provided on "AS IS" basis, without warranty
 * or guaranty of any kind.
 * Author : TechIEnjoy
 * Date: 28-November-2010
 */
import java.math.BigDecimal;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

    private static SessionFactory sessionFactory;

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();

            Address address = new Address();
            address.setHouseNumber("HS/001");
            address.setAddrLine1("Address Line 1");
            address.setAddrLine2("Adress Line 2");
            address.setPhone("3234-98878");
            VisitorDetails visitorDetails = new VisitorDetails();
            visitorDetails.setName("Visitor Johney");
            visitorDetails.setAddress(address);
            Visitor visitor = new Visitor();
            visitor.setDetails(visitorDetails);
            
            Session session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            tx.begin();
            session.saveOrUpdate(visitor);
            session.delete(visitor);
            tx.commit();
            session.close();
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}
On execution of this test code, we can have a table automatically created and populated with values as defined in the Test code. And on deleting visitor object from database, we would see that the complete row/record corresponding to this object is getting deleted, thus leaving no chance for retrieving Address object alone. Is this not a composition of objects as we know it!! Hope this helps.
If anything missed out , please let me know at techienjoy at yahoo . com
Hibernate class heirarchy mapping :
Hibernate Example on mapping
class hierarchy using various ways
of persisting into database
tables.
Hibernate Bag Mapping Example :
class mapping using Bag Tag example using Hibernate
Framework and a simple to follow steps.
Hibernate one to one mapping Example :
one to one mapping explained using an example
and Hibernate Framework.
Hibernate Insert Update control :
Hibernate Example on controlling
insert and update attributes
Hibernate Transaction on JBoss :
Explaining Transaction using Hibernate
on JBoss Application Server.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Class Hierarchy Mapping Example :
class hierarchy mapping example using Hibernate
Framework and a simple to follow steps.
Hibernate Join Example :
Using Table join explained with an example
while using Hibernate Framework.
Hibernate Named Query Example :
Named Query markup using an example
and Hibernate Framework.
Hibernate Interceptor Example :
Example on using Interceptor using Hibernate Framework
with source code explained.
Hibernate Example on Filter :
Example on using Filter using Hibernate Framework
to work with.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Hibernate Property Formula :
Hibernate Example on Property
Tag with ease to do code walk-through
Hibernate Many to Many Mapping Example :
Many to many mapping example using Hibernate
Framework and a simple to follow steps.
Example on persisting Class Hierarchy :
Example on using Hibernate Framework
to persist Class Hierarchy into database.
List of Examples on Hibernate :
List of example using Hibernate.
Hibernate Component Property :
Hibernate Example on Component 
with source code explained.
Hibernate Interview Questions :
Interview Questions on Hibernate with answer.
Hibernate Example on Filter Criteria :
Example on using Filter Criteria
using Hibernate Framework to work with.
Hibernate Example on composite Primary key :
Example on using Hibernate Framework
to work with mapping using composite
Primary key.


References :
Tags: Hibernate Class Hierarchy Persist example
Tags: Hibernate composite primary key
Tags: Hibernate criteria filter example
Tags: Hibernate Filter Example
Tags: Hibernate Interceptor example
Tags: Hibernate Interview Questions
Tags: Hibernate join example
Tags: Hibernate Many to Many Mapping Example
Tags: Hibernate mapping bag
Tags: Hibernate mapping class hierarchy table per subclass
Tags: Hibernate named query markup
Tags: Hibernate One to Many mapping example
Tags: Hibernate One To One Mapping Example
Tags: Hibernate scenario one to many
Tags: Hibernate transaction jboss
Tags: Hibernate



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions