Remote Synthesis
Search my blog:
Viewing By Entry / Main
Mar 26, 2010

Dumb ColdFusion ORM Mistakes Vol. 1 - one-to-one relationships

Given that I am finally delving deep into ORM for a real application rather tha proof-of-concepts or demos for articles, I am finally able to make idiotic mistakes in configuration that cause me days worth of grief. Thus why I am calling this post the first in a series on my dumb mistakes in the hopes that you don't have to follow suit. Today's issue had to do with configuring a one-to-one relationship and blindly overlooking what was properly in the documentation (I think I may have stared at it too long).  Here are the key items in the configuration of Employee and OfficeCubicle from the documentation example (I will note what I screwed up and what it caused after):

  • Property of child "officeCubicle" in parent "Employee" set to "one-to-one" and pointing to "OfficeCubicle" component (that one is easy I know)
  • ID in child component (OfficeCubicle) set to generator="foreign"
  • ID in child component (OfficeCubicle) set with params="{property='Employee'}" (this is not so clear, the property is the name of the one-to-one relationship property within the child component - not the name of the component necessarily)
  • Child component (OfficeCubicle) has relationship property defined as "one-to-one" and points to parent CFC (Employee) (another easy one)
  • Relationship property in child is set to constrained="true"

My dumb mistake? I had constrained="true" on the relationship property in the parent. This meant that anytime Hibernate tried to insert an Employee (in this case) that it would give me a "violation of foreign key constraint" error because a corresponding OfficeCubicle record did not yet exist. It's a totally simple and obvious rule to remember that the constraint should be on the child and not on the parent [smacks self in head]. Once I set that properly everything worked as advertised.

As a sidenote, I am using an Apache Derby Embedded database for some local testing when creating my objects (with Hibernate set to manage tables). Trying to change the objects to fix this issue caused me to get errors saying "could not export DDX." The issue, which I could see in the console, was that Hibernate created a Hibernate_Unique_Key table in my schema and it kept failing on the drop constraint and drop table for that table somehow (I think this table relates to my ID's being set to "native" but not totally sure). Anyway, simply deleting the Derby DB files and recreating them in the ColdFusion Admin seemed to resolve this (though that may not always be feasible it would be for a testing scenario like this).

Anyway, hopefully this post will stop others from repeating my stupidity...more dumb mistakes to follow I am sure. :)

Comments
Sam Farmer
I've had quite a few "slap head" moments myself setting up ORM. Its well worth getting through it though as ORM is so awesome for development.

In regards to your sidenote I've taken to doing this for my "PK" definitions:
property name="playerID" ormtype="integer" fieldtype="id" generated="always" generator="native";

There is also a great cf-orm-dev group:
http://groups.google.com/group/cf-orm-dev


Write your comment



(it will not be displayed)