Collection mappings by R4R Team

There are quite a range of mappings that can be generated for collections that cover many common relational models. We suggest you experiment with the schema generation tool so that you understand how various mapping declarations translate to database tables.
Mapping in the hibernate we used to map a collection which depends upon the type of interface. for understanding the mapping we can take a small example for this a <set> element is used for mapping properties to be set.

<class name="Product">
    <id name="serialNumber" column="productSerialNumber"/>
    <set name="parts">
        <key column="productSerialNumber" not-null="true"/>
        <one-to-many class="Part"/>
    </set>
</class>

Apart from <set>, there is also <list>, <map>, <bag>, <array> and <primitive-array> mapping elements. The <map> element is representative:

<map
    name="propertyName"                                         
    table="table_name"                                          
    schema="schema_name"                                        
    lazy="true|extra|false"                                     
    inverse="true|false"                                        
    cascade="all|none|save-update|delete|all-delete-orphan|delete-orphan"
    sort="unsorted|natural|comparatorClass"                     
    order-by="column_name asc|desc"                             
    where="arbitrary sql where condition"                       
    fetch="join|select|subselect"                               
    batch-size="N"                                              
    access="field|property|ClassName"                           
    optimistic-lock="true|false"                                
    mutable="true|false"                                        
    node="element-name|."
    embed-xml="true|false"
>

    <key .... />
    <map-key .... />
    <element .... />
</map>

Tag

Property

Name

Collection property name

Table (optional)

It is the default property name. It is used to represent to collection table. We can not use this for one to many association

schema(optional)

For override to the schema declare on the root element

Lazy (optional)

It default true. It work for the disables the lazy fetching and specifies that the association is always eagerly fetched. It can also used to enable “extra lazy” fetching where most operation do not initilized the collection.So we can say that it is suitable for large collection

Inverse(optional)

It defaults false. We have to marke this collection as “inverse” end of bidirectional association.

Cascade (optional)

It default none. We use to enable operation to cascade to child entites.

sort(optional)

It specifies a sorted collection with natural sort order or a given comparator class.

Order-by(optional)

It use the JDK1.4 only. It specifies a table columb of columns that define the iteration order of Map, Set of bag, together with an optional asc or desc.

Where(optional)

This specifies an arbitary SQL WHERE condition that is used when retriving or removing the collection. This is useful if the collection needs to contain only a subset of the available data.

fetch(optional)

It defaults to select. We can chooses between outer join fetching, fetching by sequential select, and fetching by sequential subselect.

Batch-size(optional)

Detaults to 1 and it specifies a “batch size” for lazily fetching instances of this collection.

access(optional)

It placed defaults to the property. The strategy hibernate uses for accessing the collection property value.

Optimistic-lock(optional)

It set defaults on true. This specifies that changes to the state of the collection results in increments of the owing entity's version. For one to many association we want to disable this setting.

mutable(optional)

It defaults set on the true. Here the value of false specifies that the elements of the collection never change. This allows for minor performance optimization in some cases.

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!