One-to-many associations by R4R Team

One-to-many association connect to tables of two classes via a foreign key with no intervening collection table. 

This mapping loses certain semantics of normal Java collections:

1. An instance of the contained entity class cannot belong to more than one instance of the collection.
2. An instance of the contained entity class cannot appear at more than one value of the collection index.
3. An association from Product to Part requires the existence of a foreign key column and possibly an index column to the Part table. A <one-to-many> tag indicates that this is a one-to-many association.


Information about the above code:



class (required)

the name of the associated class.

not-found (optional - defaults to exception)

specifies how cached identifiers 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.

The <one-to-many> element does not need to declare any columns. Nor is it necessary to specify the table name anywhere.

If the foreign key column of a <one-to-many> association is declared NOT NULL, you must declare the <key> mapping not-null="true" or use a bidirectional association with the collection mapping marked inverse="true". See the discussion of bidirectional associations later in this chapter for more information.

The following example shows a map of Part entities by name, where partName is a persistent property of Part. Notice the use of a formula-based index:

<map name="parts"
    <key column="productId" not-null="true"/>
    <map-key formula="partName"/>
    <one-to-many class="Part"/>
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!