Uncategorized
Using Hibernate in CF
Hibernate
is a an object/relational persistence and query service for Java.
With Hibernate you create your model using POJO’s (Plain Old Java
Objects) and configure an xml file per object to be persisted. So
with the Hibernate libraries installed its very easy to instantiate a
db session, populate your POJO, and pass that into a save method in
your session. Thats it. No inserts, no DAO code, just a
couple calls and its done. And I want to do this from my cf
apps. Seeing as there is a group devoted to just such a topic, I am not breaking any new ground here. In fact, all I have been working towards is making Matt Woodwards example
code (available to group members). And after a bundle of work I
have finally succeeded. Read on to see what I had to do to make
the demo application work.
The demo code that Matt released is a simple user manager
application. In the Application.cfm we set up our application and
then instantiate our hibernateConfig object into the application
scope. Because the hibernate configuration regards our whole
application, we dont need to worry about setting it up each request,
just once per application load. Then for the example the next
time we use hibernate is when we enter the user edit form. When
you load the edit page we create a session using our hibernateConfig
object in the application scope. Then using that seesion we pass
in the criteria to retrieve our user (ID). This will retrieve the
data from the database and actually return a user java object that is
populated with our data. We can then use the getters on the user
object to display the data for our form field. You can imagine
how the update and delete process goes. If you would like to
review the code to see what I am talking about, head over and join the
cfhibernate group on yahoo groups.
So i hope by now you are thinking this sounds like something you would
like to investigate further. I know that I was very eager to move
forward. So I followed the only instructions that were available
to me, the hibernate tutorial instructions. However as it turns
out CF already uses several of the java libraries that hibernate tells
you to install. As you might imagine this causes conflicts, and
problems arise. Also the hibernate instructions tell you you need
many more libs than are actually required for basic hibernate
functionality.
I am doing this on my mac and so the j2ee installations are the only
option. From my discussion with Matt he was able to get this
running on the standalone install of cf7, but i dont know if that was
linux or windows.
So I will walk you through what I did. First thing is to set up a
new instance of cf in jrun. You dont have to but as we are going
to be messing around in the core of CF, I figure its a good idea.
The next step is to pick up the hibernate libraries from the
Hibernate.org site. I pulled down the 3.0.4 set. Unzip
these and travel into the created folder. Inside this folder you
will see plenty of interesting things, and as yo learn more about
hibernate some of it may be of use, however at this time we just need
the hibernate3.jar file and some others inside the lib
directory. These files (listed below) will need to be put
in the {jrun-cf-instance}/cfusion/WEB-INF/cfusion/lib folder.
Please copy these files:
antlr.jar
cglib-full.jar
asm.jar
asm-attrs.jars
ehcache.jar
hibernate3.jar
jta.jar
dom4j.jar
Also for this demo application you will need the mysql-connector-j jar
from the mysql site. But with those jars in place you are now
ready to begin building applications with hibernate.
The next step is to set up our actual demo application. Once you
have retrieved the zip file from the yahoo groups site you can unzip it
into the document root for your cf instance. In that folder you
will find a sql file, which contains the structure we will need in our
test db. There is only one table and some dummy data in
there. The application uses the dsn of cfhibernate, so you will
need to set up a datasource in cf admin to connect to that new database.
The next files we need to work with are the actual hibernate config
files. In the lib directory of the cfhibernate project
(downloaded from yahoo) you will find 5 files. The one we are
most concerned with is the hibernate.cfg.xml file. In side here
you will see the configuration details for our db. This file
tells Hibernate how to talk to our database. You will need to
replace the information in the file with the same information you used
to create our cf dsn. At the bottom of that file you will find an
include of the User.hbm.xml. This is the xml file that maps how
our java object will get persisted to the database. You can take
a look at that if you are interested, but it doesnt require any
changes. These files from the lib dir need to be copied to the
classes directory in {jrun-cf-instance}/cfusion/WEB-INF/classes/.
Once you have these files in place you can restart your cf instance and
navigate your your cfhibernate project. If the lib are in the
right place the page will load and you should see a list of
users. this page uses cfquery so nothing special has happened
here yet. What you really want to do is click to edit
user. This is where I was running into trouble. But if I
managed to walk you through all the steps, and not ramble too much, you
should see a form with that users information in the input boxes.
If this is the case then you are set. you can update and delete
the users, even add new ones.
This was a long post, and I am a bit tired, so I am very sorry if I
ramble or if i have missed anything. I hope that in the near
future, i can make this into a real howto and get more into the actual
setup of the xml files and such. But i was very excited to have
this working and I wanted to share. Let me know if you run into
any troubles with my instructions and I will do my best to help you
out. Obviously I am not an expert in Hibernate issues but I have
spent several days getting this working, and i hope you will not have
to do the same.
23 May 2005 Simeon

This is very, very cool.
I’ll try to play with it soon and either give feedback or help out where I can.
Cheers,
Joe
Thanks Joe, It seems like a great way to get things done. I have done a couple simple java apps this week just to get the hang of how it works.
I am currently working on getting the config files and classes to be able to be loaded from a local directory, instead of the classes directory in the server, as not many programer get access to that.
We will see !
Just so you know these instructions worked for me, thanks! I am glad I didn’t have to modify a bunch of jars like the pervious instructions involved. I am pretty excited about what I can do now with CF and Hibernate! :)
hey Kurt,
I am really glad it worked for you! I always feel like i am rambling with my instructions, I am happy you were able to work through them.
Good Luck with Hibernate, let me know how you do.
Couldn’t you post a link to download Matt’s example? I really don’t like to sign up for anything (yahoo - ugh). But I’d like to be able to test this out.
I wish I’d found this tutorial about 3 days ago before I wasted hours and hours troubleshooting what happens when you put the hibernate.jar and libraries into {cfusion.home}/WEB-INF/lib instead of {cfusion.home}/WEB-INF/cfusion/lib! I can’t wait to try your approach out and (fingers crossed) get it working. Thanks for posting this info!
One question: you don’t actually put the Configuration object in the application scope, do you? I figured it would make more sense to store SessionFactory in the app scope and just request sessions from that as needed. So basically all of the configuration code would run just one, during setup.
I’ve also heard that you can feed Hibernate a ColdFusion datasource so that you don’t need to rely on a separate database driver and deal with connection pooling on your own. Does anyone know if this is true? What about creating a J2EE datasource through the JRun admin?
Thanks again for this awesome walkthrough!!