
560
DEVELOPING
COLDFUSION 9 APPLICATIONS
ColdFusion ORM
Las
t
upda
te
d 8/5/2010
•
Unique Foreign Key association
Primary key association
In this type of association, the primary key of one table references to the primary key of another table. That is, both the
tables share the same primary key.
The following example shows how to define this mapping.
Example
Consider the EMPLOYEE and OFFICECUBICLE example. Both the tables share the same primary key. The mapping
for these tables are as follows:
•
EMPLOYEE.cfc
<cfcomponent persistent="true" table="Employee">
<cfproperty name="id" fieldtype="id" generator="native">
<cfproperty name="firstname">
<cfproperty name="lastname">
<cfproperty name="officecubicle" fieldtype="one-to-one" cfc="OfficeCubicle">
</cfcomponent>
•
OFFICECUBICLE.cfc
<cfcomponent persistent="true" table="OfficeCubicle">
<cfproperty name="id" fieldtype="id" generator="foreign"
params="{property='Employee'}">
<cfproperty name="Employee" fieldtype="one-to-one" cfc="Employee" constrained="true">
<cfproperty name="Location">
<cfproperty name="Size">
</cfcomponent>
fieldtype=one-to-one
specifies that the property is a one-to-one property.
constrained=true
on
Employee
property in OFFICECUBICLE.cfc, means that a constraint is set on the
OFFICECUBICLE table for its ID to reference the ID in the EMPLOYEE table.
The ID of EMPLOYEE table is auto-generated. The ID of the OFFICECUBICLE table should be the same as the ID
of the Employee table. For this, set
generator="foreign"
. Foreign generator takes one parameter
'property'
as
input, which should be the relationship property name of OFFICECUBICLE entity which is 'EMPLOYEE' in this case.
Here, primary key values of related rows in both the tables must be the same. The identity generator algorithm in
the mapping for the component (whose mapped table has the constraint), must be set to foreign.
Unique foreign key association
In this type of association, the foreign key of one table references the primary key of another table, and the foreign key
column has a unique constraint. To define this relationship,
fkcolumn
attribute should be specified on the
relationship-property of the CFC whose table contains the foreign key column. The other end of relation should use
mappedby
attribute.
Syntax