Collections of values and many-to-many associations by R4R Team

Many to Many association has two side, one is owning side and non-owning side or inerse of this. In the many to many association join table specified on the owning side. We found the association is bidirectional if then it designed as the owning side. In other word if relationship is bidirectional then non owning side must use the mappedBy element of the ManyToMany annotation to specify the relation ship field or property of the owning side.The join table for the relationship, if not defaulted, is specified on the owning side.

Many to Many annotation may be used with in an embeddable class contained with an entity class to specify a relationship to a collection of entities. If the relational is bidirectional and the entity containing the embeddable class is the owner of the relationship, the non-owning side must use the mappedBy element of the ManyToMany annotaiton to specify the relationship field or property of the embeddable class.The dot (".") notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.

In many to many association requires a dedicated collection table with a foreign key column or columns, collection of element column or columns, and possibly an index column of columns.

For a collection of values use the <element> tag. For example:

        formula="any SQL expression"             



column (optional)

the name of the column holding the collection element values.

formula (optional)

an SQL formula used to evaluate the element.

type (required)

the type of the collection element.

A many-to-many association is specified using the <many-to-many> element.

        formula="any SQL expression"                       



column (optional)

the name of the element foreign key column.

formula (optional)

an SQL formula used to evaluate the element.

class (required)

the name of the associated class.

fetch (optional - defaults to join)

enables outer-join or sequential select fetching for this association. This is a special case; for full eager fetching in a single SELECT of an entity and its many-to-many relationships to other entities, you would enable join fetching,not only of the collection itself, but also with this attribute on the <many-to-many> nested element.

unique (optional)

enables the DDL generation of a unique constraint for the foreign-key column. This makes the association multiplicity effectively one-to-many.

not-found (optional - defaults to exception): specifies how foreign keys that reference missing rows will be handled: ignore will treat a missing row as a null association.

entity-name (optional)

the entity name of the associated class, as an alternative to class.

property-ref (optional)

the name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

Some basic example are given below:

A set of strings:

<set name="names" table="student_names">
    <key column="student_id"/>
    <element column="student_name" type="string"/>
A bag containing integers with an iteration order determined by the order-by attribute:

<bag name="sizes" 
        order-by="size asc">
    <key column="item_id"/>
    <element column="size" type="integer"/>

An array of entities, a many-to-many association:

<array name="addresses" 
    <key column="personId"/>
    <list-index column="sortOrder"/>
    <many-to-many column="addressId" class="Address"/>

A map from string indices to dates:

<map name="holidays" 
        order-by="hol_name asc">
    <key column="id"/>
    <map-key column="hol_name" type="string"/>
    <element column="hol_date" type="date"/>

A list of components

<list name="bikeComponents" 
    <key column="BikeId"/>
    <list-index column="sortOrder"/>
    <composite-element class="BikeComponent">
        <property name="price"/>
        <property name="type"/>
        <property name="serialNumber" column="serialNum"/>
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on 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!