May 31 2007

ColdFusion 8 + Hibernate: Woot.

Posted by Joe Rinehart at 3:54 PM
11 comments
- Categories: ColdFusion MX

I've been toying for a while with writing business models in Java with Hibernate providing ORM capability, and using ColdFusion as enterprise "glue" to create a service tier that deals in transfer / value objects consumed by Flex, Model-Glue apps, and, well, whatever else understands basic data structures and. Largely, my reason for using Java for the model has a lot to do with Hibernate: while Transfer and Reactor both work well, neither has the backing of Hibernate.

I also think this lets ColdFusion shine at what it's meant to do: provide a great language for "glueing" together everything from service tiers to chart generation.

With ColdFusion 8, this just got a lot easier. If you're up on using Hibernate, this is all it takes to get rolling in ColdFusion 8:

<!---
Load Hibernate with a given configuration file, add JARs contains classes and mappings, and list everything all Event instances.
--->


<cfset cfgFile = createObject("java", "java.io.File").init(expandPath("hibernate.cfg.xml")) />
<cfset hibConfig = createObject("java", "org.hibernate.cfg.Configuration").init() />
<cfset mappingFile = createObject("java", "java.io.File").init("relax-contact-manager.jar") />

<cfset hibConfig.configure(cfgFile) />
<cfset hibConfig.addJar(mappingFile) />

<cfset sessionFactory = hibConfig.buildSessionFactory() />

<cfset hibSession = sessionFactory.getCurrentSession() />

<cfset hibSession.beginTransaction() />

<cfset result = hibSession.createQuery("from Event").list() />

<cfoutput>
   <cfloop array="#result#" index="i">
      #i.getId()# - #i.getTitle()# - #i.getDate()#<br />
   </cfloop>
</cfoutput>

<cfset hibSession.getTransaction().commit() />

<cfset sessionFactory.close() />

If you're not up to speed with Hibernate, it's not hard to learn. One thing I'll be looking at doing over the next few months is writing a framework that'll automarshall graphs of POJOs to generated transfer object CFCs (and probably generate the AS3 classes to boot!).

Comments

Big Gary

Big Gary wrote on 05/31/07 4:19 PM

Awesome. Might I suggest not using &quot;session&quot; as a variable name in your example though, at first I wondered what exactly you were doing in the session scope ;-)
Joe Rinehart

Joe Rinehart wrote on 05/31/07 4:22 PM

Heh...good call. I'll change the name before someone tries to copy/paste this to write an app...
Jaime Metcher

Jaime Metcher wrote on 05/31/07 9:03 PM

Joe,

I'm just sneaking up on doing this in CF 7. Why is this easier in CF 8? Is it just the log4j versions? Is there so much of a difference that I really should wait for CF 8?

Jaime Metcher
Sami Hoda

Sami Hoda wrote on 06/01/07 12:57 AM

Impressive stuff Joe. Definitely worth taking a second look at now. :D
Mark Mandel

Mark Mandel wrote on 06/02/07 9:15 AM

Well, if you want to do it in CF7, then you had to use something like a URLClassloader, or JavaLoader, it's pretty nice that Hibernate will work now, just out of the box.
Cliff Meyers

Cliff Meyers wrote on 06/02/07 11:55 AM

Hey Joe, are you able to get logging output from Hibernate with CF8? When I was playing with this under CF7 I had a lot of issues with CF's log4j/commons-logging config suppressing everything that wasn't coming from CF itself. If that's been fixed under 8 that's a huge win.
Kurt Wiersma

Kurt Wiersma wrote on 06/02/07 7:23 PM

For the CFHibernate project I wrote some code to persist value object CFCs to a DB using Hibernate. It used the Persisted Maps feature of Hibernate. I would be happy to show you how I did it. The code is on the mailing list linked below.

http://tech.groups.yahoo.com/group/cfhibernate/files/
Andrew Duckett

Andrew Duckett wrote on 06/05/07 1:29 PM

Hey Joe,

You mentioned you were using Java for the model, and the reason for that was Hibernate. Have you tried using Java without an ORM? I'm curious on the success/difficulties of writing your own access layer and using Coldfusion as your display/view. If so, have you seen any added overhead with Hibernate, especially in Coldfusion applications?

I'm really interested in trying this, thanks for the post!
Andrew
Deron

Deron wrote on 06/12/07 2:14 PM

Can you Hibernate lazy load from CF 8? That was the show killer for me. CF couldn't handle the proxies. Or something. I can't quite remember.
Greg Conover

Greg Conover wrote on 07/17/07 11:55 AM

I'd be interested to see the configuration files that you use to set up Hibernate. I've been tasked with determining the viability of using Hibernate, and while I'm a hard-core ColdFusion developer, Hibernate is still a bit of a mystery in its relation to ColdFusion.
Daniel Shaw

Daniel Shaw wrote on 02/28/08 10:12 AM

We've been looking into this too. Fantastic post. This helped immensely.

Write your comment



(it will not be displayed)