Dependent objects by R4R Team

In Hibernate component is a contained object that is persisted as a value type and not an entity reference. We can understand by the term "component" it refers to the object-oriented notion of composition and not to architecture-level components. 

For example, Here below we created a model of a student like this:

public class Student 
{
    private java.util.Date birthday;
    private Name name;
    private String key;
    public String getKey() {
        return key;
    }
    private void setKey(String key) {
        this.key=key;
    }
    public java.util.Date getBirthday() {
        return birthday;
    }
    public void setBirthday(java.util.Date birthday) {
        this.birthday = birthday;
    }
    public Name getName() {
        return name;
    }
    public void setName(Name name) {
        this.name = name;
    }
    ......
    ......
}

public class Name {
    char initial;
    String first;
    String last;
    public String getFirst() {
        return first;
    }
    void setFirst(String first) {
        this.first = first;
    }
    public String getLast() {
        return last;
    }
    void setLast(String last) {
        this.last = last;
    }
    public char getInitial() {
        return initial;
    }
    void setInitial(char initial) {
        this.initial = initial;
    }
}

Here in the below example we can define to Name can be persisted as a component of Student. Name defines getter and setter methods for its persistent properties, but it does not need to declare any interfaces or identifier properties.

So Our Hibernate mapping would look like this:

   <class name="r4r.Student" table="student">
    <id name="Key" column="pid" type="string">
        <generator class="uuid"/>
    </id>
    <property name="birthday" type="date"/>
    <component name="Name" class="r4r.Name"> <!-- class attribute optional -->
        <property name="initial"/>
        <property name="first"/>
        <property name="last"/>
    </component>
</class>

The student table would have the columns pid, birthday, initial, first and last. Like value types, components do not support shared references. In other words, two students could have the same name, but the two student objects would contain two independent name objects that were only "the same" by value. 

The null value semantics of a component are ad hoc. When reloading the containing object, Hibernate will assume that if all component columns are null, then the entire component is null. This is suitable for most purposes.

The properties of a component can be of any Hibernate type (collections, many-to-one associations, other components). Nested components should not be considered an exotic usage. Hibernate is intended to support a fine-grained object model.

The element allows a subelement that maps a property of the component class as a reference back to the containing entity.

<class name="r4r.Student" table="student">
    <id name="Key" column="pid" type="string">
        <generator class="uuid"/>
    </id>
    <property name="birthday" type="date"/>
    <component name="Name" class="r4r.Name" unique="true">
        <parent name="namedStudent"/> <!-- reference back to the Student -->
        <property name="initial"/>
        <property name="first"/>
        <property name="last"/>
    </component>
</class>
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!