Viewers: in countries Watching now:
In ColdFusion 9 New Features, author and developer Dan Short has gathered together the important updates and improvements in this dynamic web application. Dan showcases the new ColdFusion Builder application throughout the course, using it to work through the new language and CFScript enhancements in this release. He shows how to use the new Server Manager to compare servers and create server groups, as well as monitor server health through notifications. He also explains the new integration with Office applications, building Excel, Word, and even PowerPoint files on the fly from ColdFusion. Finally, he covers the important new Object Relational Mapping feature and how to use the built-in Flash Player. Exercise files accompany this course.
Pulling records out of the database is fun enough, but nothing really gets done, unless you can save data back to the database. In this video I am going to show you some gotchas with the way ColdFusion 9 and Hibernate save records. I am going to show you how to properly manage when changes you have made to your objects actually get saved. So to start, let's make sure that everything is still set up correctly for our ORM. In Application.cfc, we should have ORM enabled. Our cfcs are going to be in the cfcs directory and we have our SQL logging turned on.
Open up savingrecords.cfm. All we are doing is pulling one record out of the database and dumping it back to the screen. So to make things easier to see exactly what's going on with our SQL, I am going to dropout both the left and the right- hand columns. Then I am going to move the Console up here to the upper right and collapse this bottom section. Now, I can see my code on the left and all the SQL that's getting run on the right. If I preview the file, we can see all of the hibernate SQL getting executed.
So, now that we know that's working, let's clear out the console and change our object. I will go over here and change this artist's name, set the first name to Dan. Now, in any ColdFusion 8 application, once you have changed an object you have to do something to make it save. You can call a save method. You write an update statement on the database. You perform some action that makes the application save that record. With ColdFusion 9 and Hibernate, things work a little differently.
If I preview this file, we can see that not only I did a select run, but an update ran as well. Even though I did nothing to tell the record to save. The reason this happens is because Hibernate creates an ORM session. What that means is at the beginning of a request, a session begins in Hibernate that keeps track of every change made to every object in your request. At the end of that request, all changes are flushed, which we means Hibernate checks them all and does all the work that it thinks it needs to do. But for me, this doesn't quite work.
I don't want everything updated at the end of the request. I want to have control over when records get written to the database. So, I know that they only get written when they are supposed to. To change that in the Application. cfc, we can add another setting here, called flushatrequestend. If we set that value to false, ColdFusion will never automatically flush the ORM session from hibernate. So if we go back to our savingrecords. cfm, I am going to clear the console again and reload my page.
We can now see that only the slect ran. So how I do get records to save? If we switch back over to Source view, I just have to add one function, cfset ORMFlush. This will tell Hibernate to take every change that's been written to objects so far and write it to the database. So let's clear the console once more and preview our file. Now what happened here is that the change still didn't show up. Well, the last time we loaded this file, we updated the name to Dan.
In this case, we are still updating the name to Dan, so nothing actually changed on the record. The record is not dirty, so Hibernate isn't going to save it. If we go back to our Source view and change this artist's name back to Aiden and then preview our file, now we can see that the select and the update ran. So what if for some reason I want to back out my changes? If I don't want to save this record to the database, I can evict the record from the ORM session. To evict the record from the ORM session, we first have to get our session.
So we use the ORMGetSession function and then call the Evict method and then I just have to tell it which object in the current session I want to evict. In this case, I am telling it to kick the artist out of the ORM session, so it doesn't get saved. Let's go ahead and change the first name this time to Ben. Now, well we can make sure that the object does indeed become "dirty" and we can make sure that it gets evicted from the session. Let's clear the console over here once more and then preview our file.
We can see that the first select was run, but there was no update. However, if we look over here on the code side, I can still see the object state still shows the first name Ben, but it was never saved back to the database. So now you know how to update records and cancel an update for a record. Now let's go over creating new records. Go back to our Source view. Let's comment out this first section. So to create a new record, I am going say cfset Artist = EntityNew and I will tell it I want a new copy of an artist.
Then I am going to do Artist.setArtistID. Because the Artist IDs in this particular database aren't auto-numbered, I have to enter in my own ID. If the database had an auto-number field for the ID, you wouldn't need to mess with this. Next, we'll set the Firstname to Dan, and we will set the Lastname to Short. Now, let's clear the console and see what happens when we preview our file. You will notice the no SQL was run at all.
When you insert a new record, you have to explicitly tell ColdFusion that you want it to save the record. So if we go back to our source, we need to call EntitySave and then I pass it the object I want to save and then call ORMFlush in order to get it to write the insert to the database. If I go and preview my file, we now see that there is an initial select to make sure that that record doesn't exist and then an insert is run. So you should now have a grasp of some of the hidden pitfalls of using ColdFusion 9's new ORM features when you are saving data.
Just be mindful of how and when you are saving data to make sure that you don't save something you shouldn't have and that all data is validated before pushing it into the database.
There are currently no FAQs about ColdFusion 9 New Features.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.