ColdFusion ORM Error - java.lang.Integer, etc.
Nov 28 |
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 errorerror 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
<cfcomponent displayName="Users" persistent="true" table="Users" output="no" hint="ORM logic for the new Users table">
<cfproperty name="UserId" fieldtype="id" generator="native" setter="false">
<!-- Many users per blog. --->
<cfproperty name="BlogRef" ormtype="int" fieldtype="many-to-one" cfc="Blog" fkcolumn="BlogRef">
</cfcomponent>
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 errorwould change.
Even hardcoding the value to an int causes an error:
<!-- Load the entity. --->
<cfset UserDbObj="entityNew("Users")">
<!-- Use the entity objects to set the data. --->
<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. Oftentimes, ColdFusion may always raise this cryptic error if the value is set in any other way! The following code finally solved this perplexing error:
<!-- 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. --->
<cfset BlogRefObj="entityLoadByPK("Blog"," 1)="">
<!-- Load the entity. --->
<cfset UserDbObj="entityNew("Users")">
<!-- Use the BlogRefObj entity object to set the data. --->
<cfset UserDbObj.setBlogRef(="" BlogRefObj="" )="">
Further Reading How do I store an integer using ColdFusion Orm? - by James Cushing
Tags
ColdFusion ORMThis entry was posted on November 28, 2019 at 12:30 PM and has received 1857 views.