Member Menu
 
 Monthly JBoss newsletter:
 
Hibernate Books
CaveatEmptor

Hibernate and Eclipse Integration

Recently I had the experience of converting a Java project in Eclipse 3.1 to a plugin and RCP. Previously working Hibernate activity failed with errors indicating that the hibernate.cfg.xml file could not be located. This led me on massive flailing with plugin classpaths, to no avail, since this was not the problem.

The Eclipse class loading mechanism and problems with integrating with third party libraries has been the subject of much discussion, and previous solutions have involved using Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()) in the appropriate places, which are not obvious to me nor to most naive users. However, Eclipse 3.1 has a "buddy loading" facility that solves this problem nearly trivially. Credit for telling me the solution belongs to snpesnpe, but is presented here to help with this problem.

The issue is that the Hibernate libraries cannot know, in advance, where a user will install relevant code, particularly the hibernate.cfg.xml and related hbm.xml files. Placing the hibernate.cfg.xml file at the root of the classpath is the usual solution, but since the Hibernate libraries don't even know that your plugin exists, there is no way for them to know what classpath to explore for this file. Enter the buddy system.

In the MANIFEST.MF file of the Hibernate plugin (which NEEDS the buddy loading), such as org.hibernate.eclipse, add a line:

Eclipse-BuddyPolicy:registered

and in the MANIFEST.MF file of your plugin project or RCP project, add the line:

Eclipse-RegisterBuddy:org.hibernate.eclipse

Important to notice the syntax - our plugin is willing to be seen by the hibernate library, using Eclipse-RegisterBuddy, and Hibernate is registering itself with Eclipse-BuddyPolicy. While this is stated clearly in the Eclipse Help (in retrospect!) it is critical to get the syntax precisely correct.

Finally, if you are using HibernateUtil as your main entry point into Hibernate, then in your plugin start method add the line:

Class.forName("myPlugin.HibernateUtil"); //full class name should go here

This works - assumption is that hibernate.cfg.xml is in the src directory of your plugin and this is in the classpath.


  NEW COMMENT

Hibernate-Annotation and Eclipse RCP- IllegalAccessException 22 Apr 2007, 21:44 vata
Great!
This Works for me. 

But I have another problem found. If I want to use hiberante-
annotations in my-test-rcp. and found 'IllegalAccessException' when
org.hibernate.cfg.ExtendedMappings accessing its super class 
org.hibernate.cfg.Mappings in intialization time.

Don't worry about the compatibility matrix. All libraries were came 
from distribution zip and converted into eclipse plugins. I use 
hibernate-core 3.2.3-GA and hibernate-annotation 3.2.1-GA. I also 
inculded 'Eclipse-BuddyPolicy: registered' in each dependency jars and 
list all in my-test-rcp's MANIFEST.MF files under 'Eclipse-
RegisterBuddy' and also in hibernate-core and hibernate-annotations

I think it'a about OSGI problem. CMIIW

Is there anyone has found the same problem and solved it?
 
Hibernate-Annotation and Eclipse RCP- IllegalAccessException 22 Apr 2007, 21:59 vata
POST QUESTIONS ON THE FORUM! COMMENTS HERE SHOULD ADD VALUE TO THE 
PAGE!POST QUESTIONS ON THE FORUM! COMMENTS HERE SHOULD ADD VALUE TO 
THE 
PAGE!

More information about why this is about OSGI.
The Exception throws only when running the application as Eclipse RCP 
but the junit test case is work fine as expected.

this was the exception stack trace:
java.lang.IllegalAccessError: tried to access method 
org.hibernate.cfg.Mappings.<init>
(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util
/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/List;Ljava/util/List;Lorg
/hibernate/cfg/NamingStrategy;Ljava/util/Map;Ljava/util/Map;Ljava/util/
Map;Ljava/util/List;Ljava/util/Map;Ljava/util/Map;)V from class 
org.hibernate.cfg.ExtendedMappings
	at org.hibernate.cfg.ExtendedMappings.<init>
(ExtendedMappings.java:65)
	at 
org.hibernate.cfg.AnnotationConfiguration.createExtendedMappings
(AnnotationConfiguration.java:166)
	at 
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile
(AnnotationConfiguration.java:254)
	at org.hibernate.cfg.Configuration.buildSessionFactory
(Configuration.java:1286)
	at 
net.vata.my.test.rcp.core.model.hibernate.HibernateUtils.<clinit>
(HibernateUtils.java:23)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at net.vata.my.test.rcp.ui.Application.run
(Application.java:25)
 
Another tip 29 Apr 2008, 13:14 cleland
I never got the IllegalAccessException above, but I still had some
problems that the original post didn't solve.  

In my case I was using one plugin which I'll call myPluginA (which uses
the hibernate plugin) as a plugin for another application (myPluginB) so
I didn't want to have the hibernate.cfg.xml inside the myPluginB project.

What I had to do was put the "Eclipse-BuddyPolicy: registered" line
in the manifest.mf's of both the hibernate plugin and of myPluginA

I then put the "Eclipse-RegisterBuddy: org.hibernate.eclipse, myPluginA"
in the manifest.mf of myPluginB and 
"Eclipse-RegisterBuddy: org.hibernate.eclipse" in myPluginA's manifest.mf.

I then had to put the line:

URL url = HibernateUtil.class.getResource("../hibernate.cfg.xml");  

inside HibernateUtil and then use the configure which takes a URL argument 

sessionFactory = new Configuration().configure(url).buildSessionFactory();

instead of 

sessionFactory = new Configuration().configure().buildSessionFactory();
 
Re: Another tip 31 Jul 2008, 23:29 vinant
Your issue is different you are not using AnnotationsConfiguration.

I am having the exact same stack trace with IllegalAccessException.

Has someone been able to find a resolutions ?

Please share.

-Vinant Pandey
 
Found a solution 10 Sep 2008, 22:45 sgm
It took me a while, but I found the IllegalAccessError is caused when 
you create separate Eclipse plug-ins for hibernate and hibernate-
annotations. The reason is they share a common package 
(org.hibernate.cfg) and Eclipse will load them with separate class 
loaders. In this case, a superclass cannot be accessed because it was 
loaded in a different class loader.

My fix was to place both hibernate and hibernate-annotations into a 
single Eclipse plug-in.

If you're using Eclipse 3.3+, you'll get another error regarding 
javax/management/MBeanServer which is caused because hibernate 
includes the JMX jars which are part of Eclipse. Remove them from your 
hibernate plug-in.
 
eclipse rcp + hibernate example 23 Sep 2008, 05:22 bill101
Eclipse-BuddyPolicy:registered

and in the MANIFEST.MF file of your plugin project or RCP project, add 
the line:

Eclipse-RegisterBuddy:org.hibernate.eclipse


that is great!!!
please give an example,please!
I also met such question.

thinks
bill101
 
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]