Coldfusion orm java.lang.Int error

Background

I ran into an interesting error when working on converting Galaxie Blog's database to use ORM.

When importing data from the original database, I received a very cryptic ColdFusion ORM error coldfusion orm java.lang.String error error when trying to set the value of a foreign key. It was trying to set an int, and assumed that ColdFusion somehow was casting the int to a string. I manually set the value to an int, and still received the error, but this time received Coldfusion orm java.lang.Int error. This was perplexing. This should have worked as the foreign key expected an int.

Relevant property of BlogRef

view plain about
1<cfcomponent displayName="Users" persistent="true" table="Users" output="no" hint="ORM logic for the new Users table">
2    <cfproperty name="UserId" fieldtype="id" generator="native" setter="false">
3    <!--- Many users per blog. --->
4    <cfproperty name="BlogRef" ormtype="int" fieldtype="many-to-one" cfc="Blog" fkcolumn="BlogRef">

To try to understand what was going on- I kept on trying to change the datatype, but no matter what I set the datatype to, I would receive the same cryptic error. The only difference in the error is that the datatype java.lang.thisDataType error would change.

Even hardcoding the value to an int causes an error:

view plain about
1<!--- Load the entity. --->
2<cfset UserDbObj = entityNew("Users")>
3<!--- Use the entity objects to set the data. --->
4<cfset UserDbObj.setBlogRef(1)>

Results in: Coldfusion orm java.lang.Int error

What was even more perplexing is that I had successfully used the same code in previous blocks that had worked! I have just started down the ColdFusion ORM path and wondered what the hell have I gotten myself into.

After much research, it turns out that either ColdFusion ORM or Hibernate wants an object passed to a foreign key. Often times, ColdFusion may always raise this cryptic error if the value is set in any other way!

The following code finally solved this perplexing error:

view plain about
1<!--- Load the blog table and get the first record (at this time there only should be one record). This will pass back an object with the value of the blogId. This is needed as the setBlogRef is a foreign key and for some odd reason ColdFusion or Hibernate must have an object passed as a reference instead of a hardcoded value. --->
2<cfset BlogRefObj = entityLoadByPK("Blog", 1)>
3
4<!--- Load the entity. --->
5<cfset UserDbObj = entityNew("Users")>
6<!--- Use the BlogRefObj entity object to set the data. --->
7<cfset UserDbObj.setBlogRef( BlogRefObj )>

Further Reading

How do I store an integer using ColdFusion Orm? - by James Cushing