Gregory's Blog

ColdFusion ORM Error - java.lang.Integer, etc.

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

This entry was posted on November 28, 2019 at 10:30 AM and has received 182 views.

ColdFusion Orm, a fantastic book by John Whish

ColdFusion ORM, a fantastic book by John Whish

Background

I needed to learn ColdFusion ORM as ORM supports all of the modern databases that want Galaxie Blog to be able to support. I don't want to have to incorporate different SQL logic for every database. Coding everything by hand would be an immense task, and would be problematic to test. Using ColdFusion ORM would solve this dilemma and I could use a single codebase that would automatically translate the ORM logic across various database platforms. However, learning anything new that has such a large scope is a daunting task.

Enter John Whish's ColdFusion ORM book to the rescue!

In order to embark on my re-coding effort, I ordered and read a ColdFusion ORM book written by John Whish and found his book to be an invaluable resource. The book is concise and well laid out. It starts by answering simple questions, such as What is ORM? and provides a general background. His book shows you how to configure ORM, explains that ORM is used for CRUD operations, and then gets into the meaty topics such as ORM relationships and HQL. In the final part of the book John discusses validation, caching and provides helpful tips.

If you're using ORM and ColdFusion, I found his book to be an invaluable resource.

Related Resources

This entry was posted on November 22, 2019 at 11:44 AM and has received 171 views.




Footer Logo

Your input and contributions are welcomed!

If you have an idea, BlogCfc based code, or a theme that you have built using this site that you want to share, please contribute by making a post here or share it by contacting us! This community can only thrive if we continue to work together.

Images and Photography:

Gregory Alexander either owns the copyright, or has the rights to use, all images and photographs on the site. If an image is not part of the "Galaxie Blog" open sourced distribution package, and instead is part of a personal blog post or a comment, please contact us and the author of the post or comment to obtain permission if you would like to use a personal image or photograph found on this site.

Credits:

Portions of Galaxie Blog are powered on the server side by BlogCfc, an open source blog developed by Raymond Camden. Revitalizing BlogCfc was a part of my orginal inspiration that prompted me to design this site. Some of the major open source contributers to BlogCfc include:

  1. Peter Farrell: the author of 'Lyla Captcha' that is used on this blog.
  2. Pete Freitag: the author of the 'ColdFish' code formatter that is also used on this blog.

Version:

Galaxie Blog Version 1.50 November 22 2019