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!).
Big Gary wrote on 05/31/07 4:19 PM
Awesome. Might I suggest not using "session" as a variable name in your example though, at first I wondered what exactly you were doing in the session scope ;-)