Saturday, 18 September 2010

Hibernate in OSGi is a definite NO NO!

Recently I had the honor of trying to switch the persistence provider of our application so we can debug some performance issues and to my horrible suprise, there is no standard OSGi bundles for hibernate. Wow, with eclipselink, it was just a single jar! easy to find on eclipselink website, drop it in your auto start folder and bang! you are on your way.

After spending all day googling, I found some out of date bundles in SpringSource's repository. Wow, how difficult is it to produce an OSGi bundle if you have managed to write the code. I was thinking its just a packaging thing. Maybe am mistaken.

Anyway, after spending all that time, downloading all the gazillion dependencies manually and copying them to the felix auto start folder and also adding them to my pom file, the application finally started with all dependencies resolved.

As if that was not enough , I got an exception on startup
No Persistence provider for EntityManager named ...blah blah
That was not enough? the persistence.xml file was there. The provider class specified in that file was there. I could clearly see it in Netbeans by just expanding the libraries tree and navigating the packages and the jar file that contained that class was also in the felix auto deploy folder.

I then started another googling session. It turns out hibernate has problems with classpath in OSGi. Ypu have to use some thread hacking to make it see your META-INF/persistence.xml and when you finish reset the classpath to what iwas before. At that point I just gave up. Its not worth the effort. As much as I love hibernate and hibernate-Search, its just not ready for OSGi. Will reinvent my search wheel with eclipselink


Mario Alejandro said...

check this out:

maybe it would help you

cameo said...


Thanks very much for the link, looks like a solution to my problem. Really appreciated

Sudheer Krishna said...

I totally agree that the biggest two issues when integrating with other 3rd party libraries are the following.

Be careful while loading any class and use specific classloader to load the class than just Class.forname() [Either ensure this class is exposed through export package or use TCCL to laod the class]