13. Collections of Components
Definition: Collections of components are also possible. These can be especially useful when dealing with a complex top-level collection.
Scenario: We have one first-rank class, Foo, and a top-level collection of second-rank class FooSecond which Foo holds.
Hibernate Mapping:
In Hibernate, this could be mapped as follows:
Table Schema:
This time we have two tables. Foo has a seconds_id FK column and Seconds appears as a collection table in its own right. The table structure is the same as for a standard top-level collection, but this time Seconds is being treated as a component object - a composite of two columns in the collection table - and set as a single property of Foo.
Bidirectionality:
There's no bidirectional relationship available here as there is only one first-rank class involved.
Definition: Collections of components are also possible. These can be especially useful when dealing with a complex top-level collection.
Scenario: We have one first-rank class, Foo, and a top-level collection of second-rank class FooSecond which Foo holds.
|
Set Foo.getSeconds() // returns a collection of FooSecond instances |
Hibernate Mapping:
In Hibernate, this could be mapped as follows:
|
<set role="seconds"> <key column="id" type="string"> <generator class="uuid.hex"/> </key> <composite-element class="FooSecond"> <property name="firstName"/> <property name="lastName"/> </composite-element> </set> <class name="Foo" table="foo"> ... <collection name="seconds" column="seconds_id" role="seconds"/> </class> |
Table Schema:
|
Foo |
|
|
id |
seconds_id |
|
Seconds |
||
|
id |
firstName |
lastName |
This time we have two tables. Foo has a seconds_id FK column and Seconds appears as a collection table in its own right. The table structure is the same as for a standard top-level collection, but this time Seconds is being treated as a component object - a composite of two columns in the collection table - and set as a single property of Foo.
Bidirectionality:
There's no bidirectional relationship available here as there is only one first-rank class involved.
