Search This Blog

Sunday, 30 October 2011

Component of Components

Hibernate allows us to compose our components out of other components. This is making extreme use of java in the SQL world, but provides us with a great deal of flexibility. I modified the address class in the previous example to be composed of components.
So the address class now looks like
public class Address {
    private Person person;
    private String city;
    private Location location;
    private ZipCode zipCode;
The components placed inside Address class are as follows:

public class Location {
    private Street street;
public class Street {
    private String name;
public class ZipCode {
    private String zipCode;
The database table is unchanged.However the mapping file is modified to the following:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.model.components.bidirectional">
    <class name="Person" table="PERSON">
        <id name="id" type="long">
            <column name="ID" />
            <generator class="native" />
        <property name="name" type="string">
            <column name="NAME" />

        <component name="address" class="ComponentAddress">
            <parent name="person" />
            <property name="city" type="string" not-null="true">
                <column name="CITY" />
            <component name="location" class="Location">
                <component name="street" class="Street">
                    <parent name="person" /> 
                    <property name="name" type="string" not-null="true">
                        <column name="STREET" />
            <component name="zipCode" class="ZipCode">    
                <property name="zipCode" type="string" not-null="true">
                    <column name="ZIP_CODE" />
I have highlighted the component nesting levels in the code above. As can be seen the back-link is not only present for the outer-most component but also for inner component Location. Backlinks always refer to the entity and not the class that includes the component instance !!
I decided to execute an HQL query to retrieve a Person object for a given Street name

static void testHqlRetrieval() {
    Session session = SESSION_FACTORY.openSession();
    try {
        //get Entity with query on component
        Query query = session.createQuery("from Person p where = ?");
        query.setString(0, "Aundh Road");
        Person person = (Person) query.list().get(0);
        System.out.println("Person : " + person + " City : " 
              + person.getAddress().getCity());
        System.out.println("Inner person back -reference " 
              + person.getAddress().getLocation().getStreet().getPerson().getName());
    } catch (HibernateException e) {
    } finally {
The code for back-reference is crooked to say the least, but it works ;P The logs indicate the SQL query fired

3016 [main] DEBUG org.hibernate.SQL  -  
        person0_.ID as ID0_,
        person0_.NAME as NAME0_,
        person0_.CITY as CITY0_,
        person0_.STREET as STREET0_,
        person0_.ZIP_CODE as ZIP5_0_ 
        PERSON person0_ 

No comments:

Post a Comment