Search

Subscribe

Enter your email address to subscribe to this blog.

Recent Comments

Intro to Spring Security Core for Grails
Eric Pierce said: Thanks for these screencasts, Dan! You made it crazy simple to get up and running w/security core h... [More]

Removing duplicates from an array of objects
Arvind said: Great tip, can't thank you enough for this. [More]

Intro to Spring Security Core for Grails
Santosh said: Thanks much for putting up these screencasts. As the others here I'm a beginner and I've been having... [More]

Grails Spring Security Plugin - Logout postOnly setting
eriihine said: I still had some issues with this one. It seems that the href link is always generating a GET method... [More]

Intro to Spring Security Core for Grails
Dan Vega said: Just a heads up but I decided to write up a quick post on your question just in case it trips up any... [More]

ColdFusion ORM calculated values

A calculated value is a read only property that is calculated using sql expressions. In this example we are building a blog application that has posts and comments. First we have a post entity. I have stripped these down for this example so we can look at the root of the issue. And then we have a comment entity.

So far this is pretty basic stuff, nothing really special going on here. Now we need to grab a record from our database and display it, easy enough. There is a major problem here with our display though. How would we get a count of the number of comments for this post? You could count the array of comments. While this would work your going to have to do this 2x on the post display page now. One time for the comment count and then a 2nd time to display the comments. In a post where there is only 1 or 2 comments this will not be a big issue but you can imagine the trouble this would cause with a large number of comments. A better solution to this problem is to use a calculated value. We can actually create this commentCount as a property of our post entity. All we are doing here using SQL to the count. The id of the Post Entity is id (comes from the base Entity) so we can do a lookup in the comment table based on post_id being equal to id. Now if we want to output the count all we have to do is.

 
 

EntityNew properties argument

This is a going to be a real short post but I think its a great little time saver. In ColdFusion 9.0.1 they added a second argument for the entityNew function. Now you can pass in a structure of properties and the setters will be called when the entity is instantiated. So before we had to write code that looked like this. And now we can write code that looks like this.

 
 

Hibernate Statistics in ColdFusion

One way to debug Hibernate is to log the generated sql, another way is by enabling live statistics. By default ColdFusion has live statistics disabled and for good reason. This is not something you want running in production and its only meant for development. The easiest way to get statistics out of the Hibernate engine at runtime is the Session Factory. Fortunately for us ColdFusion has exposed this to us by using the ormGetSessionFactory() method.

If you run the following code you will see all of the different statistics methods exposed to you.

There are statistics for things like

  • Global Statistics
  • Entity Statistics for a particular entity
  • Collection Statistics for a particular collection role
  • Query Statistics for SQL & HQL queries
  • Second Level Cache Statistics for detail runtime information about a particular region in the optional second-level cache
If you try and call any of these methods right out of the gate they are not going to work. If you remember I said earlier that statistics are not enabled by default, but we can enable them pretty easily. Now when you call any of the stats methods we will start to see some data. There is also a continent method to find out if they are enabled.

Finally we could take this and add some debugging output to application by using the on request end method in our application. The hibernate_stats.cfm could have some output with any or all of the statistics methods.

 
 

ColdFusion ORM Naming Strategy

In a perfect world of software development where a database is involved the naming conventions for tables and columns are standard. As you are aware that is not always the case. Sometimes we deal with really long or unconventional naming strategies. Fortunately for us hibernate has a way of dealing with this and ColdFusion, like everything else it does has made this really for us to use.

Let's pretend for our demo here that we are doing some work for ABC Company and that have this weird naming strategy where all of there table names are prefixed with tbl_ and all of their column names are prefixed with col_. I think you get the idea. If we were to create an entity based off of this table we would end up writing code that looked like this.

Continue Reading >>

 
 

Event Handlers & Remote Methods

Working on a project today I came across a strange issue so I thought I would get everyones opinion on it. I am not really sure if its something I am doing wrong, a bug or maybe I am just missing something. In this application I have event handling turned on.

So for nothing special and up until this point everything has been working fine. Today I am adding some ajax via everyone's favorite framework (jQuery of course) and I came across something. This is my ajax call to my remote method. And my remote method that does a join to pull all users who have a role id equal to the argument passed.

If you were to turn event handling off this works fine but with it on I am seeing the following error.

coldfusion.runtime.TemplateProxy$InvalidRemoteAccessException: The method 'preLoad' in component D:\websites\extranetsteriscom\cfc\com\*****\*****\events\EventHandler.cfc cannot be accessed remotely.
Now in my event handler I have preLoad defined (because you have to when you implement another component) but it's not doing anything. Am I to believe that If I use event handling in my application I can't use ORM for any remote functions? I can certainly get down and write some sql but thats no fun :) Anyways, just thought I would share this and get your thoughts.

 
 

More Entries