By Peter Bell

Hibernate in CF

Recently there have been some questions as to whether to close down the CF Hibernate Yahoo group as there hasn't been much traffic. But it seems to me like there is still a reasonable amount of latent interest in Hibernate in CF and from what I understand it should be easier in CF8.

I know Joe posted on this recently, but is anyone using Hibernate for real as the persistence mechanism for CF apps? I know Kurt has also talked about persisting VO's from CF to Hibernate, but has anyone got a real world app working end to end?

If so, are you using Kurts code for persisting the CFC VO’s? How do you handle lazy loading in CF using Hibernate? Are there any event notifications in Hibernate and how hard is it to “handle” them in ColdFusion? How do you handle the errors returned by Hibernate? Anyone got any sample code to share beyond the single listing by Joe and the one by Kurt? Any performance issues? And issues logging/debugging Hibernate issues?

Comment below or join the Yahoo group and let us know what you've been doing!

Comments
I have used different approach. While creating my own hibernate wrapper I decided user will not write POJOs - user would like writing CFCs. So with my approach all you need is CFC and hibernate xml mapping file. My wrapper also obtains DB connection from ServiceFactory so the connection is managed by ColdFusion.
Most interesting stuff is how CFCs are persisted - when passing CFC instance to load(), update(), save(), saveOrUpdate() or delete() method of the wrapper it checks if corresponding Java class exists. If not CF generates Java source file, generates the class. It is quite fast process and done only if class file doesn't exist.

But on the other hand - when considering Hibernate why would I consider CF? When all I need is few if statements, some calculation I would not use CF for that when using Hibernate. When I need generate PDF files or call webservices then of course yes.
# Posted By Radek | 2/28/08 10:18 AM
Other areas that are doing Java are using spring and hibernate. We have looked at using it and had Joe present on the subject. We have discussed using it but have not made a decision. It would sure help a lot with the model and cross db/dsn transactions. I agree it should be easier and Joe pointed out some sticky points that could be smoother in CF. Adobe should listen to him. I think it would be a huge deal for CF to easily be able to integrate with hibernate.
# Posted By Ryan TJ | 2/28/08 1:15 PM
Well, I'm not sure if the wishlist process for CF9 is still open, but I passed along the input as I'd love to be able to do this easily in CF.

Thanks for the input!

Can you remember what specifically were the issues that Joe was highlighting?
# Posted By Peter Bell | 2/28/08 2:03 PM
>is anyone using Hibernate for real as the persistence mechanism for CF apps?

Kind of. We model the entire domain layer in Java and just expose a service API, so CF isn't directly talking to Hibernate.

>If so, are you using Kurts code for persisting the CFC VO’s?

Nope.

>How do you handle lazy loading in CF using Hibernate?

As in keeping the session open? We use Spring, and wrap each request in Spring's openSessionInViewInterceptor. You can also configure this interceptor into JRun, but on CF Standard that means every application will use the same session factory.

> Are there any event notifications in Hibernate and how hard is it to “handle” them in
> ColdFusion?

Not an issue - they stay within the domain layer.

>How do you handle the errors returned by Hibernate?

In theory we should translate and rethrow exceptions on the Java side, but at the moment we just catch them, e.g. catch (org.hibernate.validator.InvalidStateException e) {}

>Anyone got any sample code to share beyond the single listing by Joe and the one by Kurt?

<cfscript>
context = CreateObject("Java", "org.springframework.context.support.ClassPathXmlApplicationContext");
contextPaths = ["config/applicationContext.xml","config/datasource.xml"];
context.init(contextPaths);

service = context.getBean("UserService");
user = service.getUser("jmetcher");
up = user.getUserProfile();
</cfscript>

<cfoutput>#user.getId()# #up.getFirstName()# #up.getLastName()#</cfoutput>

Obviously we'd cache the context in the Application scope. Also, I've left out the interceptor code - it's really just plumbing. Beyond that, all the interesting stuff happens on the Java side, which is just pretty standard Spring/Hibernate.

>Any performance issues?

Orders of magnitude faster than a CF domain layer.

> And issues logging/debugging Hibernate issues?

No. Being able to unit test in Eclipse with JUnit is pretty sweet. If something does go wrong when deployed to the app server, the exception log tells all. Then 99% of the time we just go back to offline unit testing to debug the problem.

Jaime
# Posted By Jaime Metcher | 2/28/08 11:38 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.