Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened. But I have become a passionate and pretty good web developer. I code mostly in ColdFusion and jQuery. Sam is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

Using structures/maps instead of arrays in ORM

05.27.2011
| 6946 views |
  • submit to reddit
By default an ORM relationship is an array. Using a structure (map for non-ColdFusion programmers) is very easy and provides nice options. Lets start with a simple relationship between of users and addresses.
component persistent="true" {
property name="userId" fieldtype="id" generated="always" generator="native";
property name="name" ormtype="string";

property name="address" fieldtype="one-to-many" cfc="address" fkcolumn="userId" inverse="true";
}
Address.cfc
component persistent="true" {
property name="addressId" fieldtype="id" generated="always" generator="native";
property name="type" ormtype="string";
property name="street" ormtype="string";
property name="city" ormtype="string";
property name="state" ormtype="string";

property name="user" fieldtype="many-to-one" cfc="user";
}

A single user object with two addresses would look like this:


Now, if we want to find out if the user has a work address we need to loop through getAddress(). Thats ok but by converting the relationship to a struct its possible to then use structure functions. Here is the relationship in User.cfc defined to return a structure:

property name="address" fieldtype="one-to-many" cfc="address" fkcolumn="userId" inverse="true"
    type="struct" structkeycolumn="type";

A dump of the user object now looks like:


Which means we can use code like this, in particular line 3:

<cfset u = entityLoad("user")>
<cfloop array="#u#" index="user">
    <cfif structKeyExists( user.getAddress(), "work" )>
        <cfdump var="#user.getAddress()["work"]#">
    </cfif>
</cfloop>

For reference here is the Application.cfc to set up Hibernate/ORM:


component
{
this.name="structsDemo";
this.ormEnabled=true;
this.datasource="cfartgallery";
this.ormsettings = { dbcreate="update" };

}
References
Published at DZone with permission of Sam Farmer, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Mark Unknown replied on Fri, 2011/05/27 - 9:31am

Can dzone possibly get a Coldfusion Zone and have these sorts of posts put under it versus the Java Zone? I read the title and i was like ... what is he talking about? Hibernate uses collections and so does JPA ... Then I opened it and saw Coldfusion and said oh, that is why. Sigh.

Kathy John replied on Tue, 2012/02/21 - 1:23pm

That's an excellent post thanks. I often forget that you can use structs for relationships, probably because I never really put much thought into the possible use cases for it.

This makes really great sense, and is an excellent approach.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.