0

Hibernate Programmatic Configuration

asked 2009-02-04 06:12:36 +0800

InfoSec812 gravatar image InfoSec812
96

Hello all. I am writing an Open Source help desk application (http://code.google.com/p/triage/). Upon first accessing the web app, the user is going to be prompted to configure the default settings for the application. Part of this is configuring the database connection settings. Since I am using Hibernate, I need to configure the Listeners for the Hibernate session handling. I figured out that I can load Listeners at run time using "desktop.getWebApp().getConfiguration().addListener()", but for the Hibernate listeners, it searches for the Hibernate.cfg.xml file. Since I am using a programmatic configuration of Hibernate, there is no Hibernate.cfg.xml file. Can anyone give me an idea of how I can load this listener such that it doesn't look for the Hibernate config file?

Thanks,

Deven

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2009-02-04 11:42:30 +0800

InfoSec812 gravatar image InfoSec812
96

OK, I think I have an answer. If I created my own version of the org.zkoss.zkplus.hibernate.* classes, I can extend them to detect the already initialized Hibernate session factory. I believe I will configure the application to store the SessionFactory object in the InitialContext of the application and have those classes check there for it. Does anyone have a better idea?

link publish delete flag offensive edit

answered 2009-02-04 13:02:10 +0800

InfoSec812 gravatar image InfoSec812
96

updated 2009-02-04 13:06:35 +0800

Woohoo! It works. You can see the changes I made to the HibernateUtil and OpenSessionInViewListener classes at:

http://code.google.com/p/triage/source/browse/#svn/trunk/Triage/src/com/blogspot/devenphillips/helpdesk/zk/hibernate%3Fstate%3Dclosed

The basic concept is that after performing a programmatic configuration of Hibernate, I store the SessionFactory object as an attribute in the WebApp. I then perform an addListener() call to add the Hibernate listeners. The modified HibernateUtil checks the WebApp object for the existence of the attribute and uses it, if it is available. I also had to make a change to OpenSessionInViewListener to handle the case that no Transactions are active at a certain point. It probably would not have caused a problem, but why clutter up the logs with unnecessary stack traces.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-02-04 06:12:36 +0800

Seen: 275 times

Last updated: Feb 04 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More