Collection Mapping
Collection mapping is similar to a one-to-many relationship mapping. However, in collection mapping, you have a collection of values instead of a collection of persistent target objects.
Consider the Artist-Art tables. If you want each Artist object to contain an array of artwork names instead of artwork objects, collection mapping should be used.
To define collection mapping in the CFC, use fieldtype="collection" in the cfproperty tag.
The collection can either be Array or Struct.
Array
Syntax
fieldtype="collection" |
Example
If each Artist object contains an array of artwork names instead of artwork objects, this mapping can be defined in Artist.cfc as:
<cfproperty name="artNames" fieldtype="collection" type="array" table="ART" fkcolumn="ARTISTID" elementcolumn="ARTNAME" elementtype="string"> |
Attribute
Attribute |
Req/Opt |
Default |
Description |
---|---|---|---|
batchsize |
Optional |
|
An integer value that specifies the "batchsize" for fetching uninitialized collections. For details, see Lazy Loading. |
elementColumn |
Required |
|
Specifies the column name that contains the data to be fetched for collection. |
elementtype |
Optional |
String |
Data type of the selected column. See ORM data types in Map the properties for details. |
fieldtype |
Required |
|
Should be "collection". |
fkcolumn |
Optional |
|
The foreign key column in the specified table.If you do not specify the foreign key column and useDBForMapping is true in ormsettings, ColdFusion automatically determines a foreign key column after inspecting the database. |
lazy |
Optional |
true |
Specifies if loading is to be done lazily:truefalseSee Lazy Loading for details. |
name |
Required |
|
Name of the collection. |
optimisticlock |
Optional |
true |
Specifies the locking strategy.truefalse |
orderBy |
Optional |
|
Specifies the Order By string. |
readonly |
Optional |
false |
truefalseIf set to true, it indicates that the collection never changes and can be cached. |
table |
Required |
|
Name of the table from where the values will be fetched. |
type |
Optional |
array |
Specifies if the collection type is:arraystruct |
Struct
Syntax
name="field_name" |
Attribute |
Req/Opt |
Default |
Description |
---|---|---|---|
batchsize |
Optional |
|
An integer value that specifies the "batchsize" for lazily fetching instances of this collection. |
elementcolumn |
Required |
|
Specifies the column name that contains the data to be fetched for collection. |
elementtype |
Required |
|
Data type of the value. See ORM data types in Map the properties for details. |
fieldtype |
Required |
|
Should be a collection. |
fkcolumn |
Optional |
|
The foreign key column in the table.If foreign key column is not specified and useDBForMapping is set to true in ORMSetting, ColdFusion automatically determines the Foreign Key column after inspecting the database. |
lazy |
Optional |
true |
Specifies if loading is to be done lazily: truefalseSee Lazy Loading for details. |
name |
Required |
|
Name of the collection property. |
optimisticlock |
Optional |
true |
truefalse |
orderby |
Optional |
|
Specifies the Order By string. |
readonly |
Optional |
false |
Value are:truefalseIf you set it to true, the collection never changes and can be cached. |
structkeycolumn |
Required |
|
Column name in the table that will be used as key of struct. |
structkeyType |
Required |
|
Specifies the data type of the key, when type=struct. For the entire list of data types, see the Data Type section. |
table |
Required |
|
Name of the table from where the collection will be fetched. |
type |
Optional |
array |
Specifies if the collection type is: arraystruct |
Inheritance mapping
If the object you need to persist has a hierarchy, the CFCs of that object hierarchy need to be mapped to the relational tables such that the entire hierarchy is persisted.
There are multiple strategies followed for inheritance mapping:
- Table per hierarchy
- Table per subclass without discriminator
- Table per subclass with discriminator
Table per hierarchy
In this model, the object hierarchy is persisted in a single table. This table includes columns for all the properties of all the CFCs in the hierarchy. The concrete subclass represented by a row is identified based on the value of the discriminator column. In this strategy, all the CFCs of the hierarchy must have the same table name.
Note: If the discriminator column and discriminator value is not specified, a default discriminator column name and value is picked up. |
Example
The following example demonstrates an implementation of table per hierarchy: