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
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.
|