Bidirectional associations with indexed collections by R4R Team

In this section we have some extra concidration for bidirectional mappings with indexed collections. In this concidration where one end is represented as a <list> or <map>.It used when using Hibernate mapping files. When we want to use the property of child class then we need to map that classs to the index column. we can use as inverse="true" of the following mapping:

Example of the Bidirectional association with indexed collection:

<class name="Parent">
    <id name="id" column="parent_id"/>
    ....
    <map name="children" inverse="true">
        <key column="parent_id"/>
        <map-key column="name" 
            type="string"/>
        <one-to-many class="Child"/>
    </map>
</class>

<class name="Child">
    <id name="id" column="child_id"/>
    ....
    <property name="name" 
        not-null="true"/>
    <many-to-one name="parent" 
        class="Parent" 
        column="parent_id"
        not-null="true"/>
</class>

In the hibernate If there is no such property on the child class, the association cannot be considered truly bidirectional. So That is, there is information available at one end of the association that is not available at the other end. In this case, cannot map the collection inverse="true". Instead,of we could use the following mapping:

Example of Bidirectional association with indexed collection, but no index column

<class name="Parent">
    <id name="id" column="parent_id"/>
    ....
    <map name="children">
        <key column="parent_id"
            not-null="true"/>
        <map-key column="name" 
            type="string"/>
        <one-to-many class="Child"/>
    </map>
</class>

<class name="Child">
    <id name="id" column="child_id"/>
    ....
    <many-to-one name="parent" 
        class="Parent" 
        column="parent_id"
        insert="false"
        update="false"
        not-null="true"/>
</class>

We need to remember that is, the collection-valued end of the association is responsible for updates to the foreign key.
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!