Monday, March 21, 2011

ColdFusion ORM is Case Sensitive

Obviously, right? Everyone that is using Hibernate to do ORM in any of their projects already knows this. Normally, this isn't a big deal. However, some of my coworkers came across an error this last week that had us scratching our head for a few minutes. I have reproduced the error using the Bookclub derby sample database that ships with ColdFusion. Maybe you'll spot the problem:

application.cfc
component {

 this.name = "ORMTEST";
 
 this.ormEnabled = true;
 this.ormSettings.datasource="bookclub";
 this.ormSettings.flushAtRequestEnd = false;
 
 public boolean function onRequestStart() {
  ormReload();
  return true;
 }

}
Book.cfc
component persistent="true" table="BOOKS" {
 property name="id" column="BOOKID" generator="increment";
 property name="title" column="TITLE";
 property name="description" column="BOOKDESCRIPTION";
 property name="genre" column="GENRE";
 property name="spotlight" column="ISSPOTLIGHT";
 property name="author" fieldtype="many-to-one" cfc="ORMTest.Author" fkcolumn="AUTHORID";
}
Author.cfc
component persistent="true" table="AUTHORS" {
 property name="id" column="AUTHORID" generator="increment";
 property name="firstName" column="FIRSTNAME";
 property name="lastName" column="LASTNAME";
 property name="spotlight" column="ISSPOTLIGHT";
 property name="biography" column="BIO";
 property name="books" fieldtype="one-to-many" cfc="ORMTest.BOOK" fkcolumn="AUTHORID";
}
Did you spot it? It took us a while, and it only occurs with HQL queries. With the above code, if you run ORMExecuteQuery("select book from Book as book") you should get the error. Running the EntityLoad() function will not cause the error. We actually turned on the saveMapping setting to see the hibernate files that were being created. At that point it was pretty clear what the issue was. The mapping that was created had the Book entity created with an entity name of BOOK. Typically, the entity name, if not explicitly stated, will be the name of the component (and in the same case). However, in this case the entity name is being created based of the property in Author.cfc. Luckily the fix was pretty easy, change the property on the Author entity to be the correct case. We actually had to delete our class files and clear the template cache to get the change to take effect.

I'm not sure why ColdFusion chooses to use the property off the Author.cfc to determine the entity name, but it was pretty confusing. I think that I would have preferred that a hard error were thrown in this situation instead of getting the runtime error.

1 comment:

  1. wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.
    ORM

    ReplyDelete