-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Using Entitymanager in an Eclipse RCP Application
PostPosted: Tue Sep 20, 2005 3:04 am 
Newbie

Joined: Tue Sep 20, 2005 2:24 am
Posts: 1
Location: Zurich, Switzerland
To get the entitymanager working in an eclipse rcp application I made the following changes in the current source which I got from the cvs

1. Modification
---------------
reason: does not find the resource

file: Persistence.java
package: javax.persistence
method: findAllProviders()

old:
Code:
Enumeration<URL> resources = loader.getResources("META-INF/services/" + PersistenceProvider.class.getName() );


new:
Code:
Enumeration<URL> resources = Persistence.class.getClassLoader().getResources("META-INF/services/"+ PersistenceProvider.class.getName());



2. Modification
---------------
reason: does not find the resource

file: Ejb3Configuration.java
package: org.hibernate.ejb
method: createEntityManagerFactory()

old:
Code:
Enumeration<URL> xmls = Thread.currentThread().getContextClassLoader().getResources( "META-INF/persistence.xml" );

new:
Code:
Enumeration<URL> xmls = getClass().getClassLoader().getResources("META-INF/persistence.xml");



3. Modification
---------------
reason: the url protocol bundleresource: could not be resolved

file: Ejb3Configuration.java
package: org.hibernate.ejb
method: createEntityManagerFactory()

old:
Code:
URL url = xmls.nextElement();

new:
Code:
import org.eclipse.core.runtime.Platform;
.....
URL urlOriginal = xmls.nextElement();            
URL url = Platform.asLocalURL(urlOriginal);



The 3rd modification is for sure the biggest hack but finally it worked. I'm not sure if there would be a better solution because I do Java programming only since a few months.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
-1 on 1. and 2. since this change will break on J2EE or any correctly defined environment. The thread context class loader has to be correctly set by the environment.
as of 3. well I'm not a big fan of importing eclipse... I'll check if there is any alternative.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:23 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
alternative is simple - buddy allocator - give me example (favorite from hibernate test) what you want do with entity manager and I send complete example rcp application with hibernate.For example, eclipse entitymanager work fine in jsr220 project (in milestone 1)
There are problems in this milestones, but it is wst rtp problem, but no hibernate

I make currently rcp,swing,hibernate,spring application and all is fine - classloder have problems with jasperreports (jasper create and compile classes in memory and it confuse eclipse, but problem is eclipse classloader)

I think that hibernate don't need work too much with classloaders, because different servers (or eclipse) do classloading and they will been confused


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 6:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
snpesnpe wrote:
alternative is simple - buddy allocator - give me example (favorite from hibernate test) what you want do with entity manager and I send complete example rcp application with hibernate.


That would be cool to have a wiki page explaining this shortly :-D

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 8:07 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
we have wiki page for buddy classloader (no allocator, sorry)

this is thread http://forum.hibernate.org/viewtopic.php?t=944666
and this is wiki http://hibernate.org/311.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 11:04 pm 
Newbie

Joined: Thu Nov 18, 2004 6:01 pm
Posts: 17
I've been working on something a little different from what is in the wiki above. The wiki talks about using hibernate core from an rcp plugin. I have been working on using a plugin that wraps entitymanager and annotations (EJB and ANN) in a plugin, then writing an rcp plugin on that.

EJB's javaxPersistence class needs to find its META-INF/services/javax.persistence.spi.PersistenceProvider resource. When my application rcp plugin calls into the EJB plugin, the context loader used by EJB is my plugins context loader, not the EJB plugin's class loader. This means that if my plugin doesn't have the PersistenceProvider resource on classpath, it won't be found by EJB library.

To fix, I do:
Code:
ClassLoader save = null;
try {
  // org.hibernate.eclipse.EJB3 would be the name of the plugin containing EJB and ANN
  Bundle ejbBundle = Platform.getBundle("org.hibernate.eclipse.EJB3");

  // loads the class using EJB3's classloader
  Class c = ejbBundle.loadClass("javax.persistence.Persistence");

  // switch context loader to EJB3's classloader   
  save = Thread.currentThread().getContextClassLoader();
  Thread.currentThread().setContextClassLoader(c.getClassLoader());

  // reflection to invoke the createEntityManagerFactory
  Method create = c.getMethod("createEntityManagerFactory", String.class);
  emf = (EntityManagerFactory)create.invoke(null, nm);

} catch (Exception e) {
  // honor the exception from javax.Persistence.
  if(e.getCause() instanceof PersistenceException)
     throw (PersistenceException)e.getCause();
   throw new RuntimeException(e);
} finally {
  if(save != null)
   Thread.currentThread().setContextClassLoader(save);               
}



The wiki referenced above helped me to understand eclipse class loader buddy. I is necessary to use classloader buddy in this plugin arrangement too. EJB plugin needs to access my rcp app's classpath to get persistence.xml file. So EJB plugin has "Eclipse-Buddy: registered" in its manifest, and my app rcp has "Eclipse-RegisterBuddy: org.hibernate.eclipse.EJB3"

I'm having trouble dealing with the bundleresource url. The wiki and the jira issue mentioned above doesn't appear to be using EJB so it was not observed. First problem is the mechanism in EJB for getting the jar url (JarVisitor) doesn't work for eclipse 3.1 bundleresource url. You might get it to work for exploded jars (Please note that getAsLocal isn't guaranteed to always return a nice file: url), but it plainly won't work for zipped jars. Eclipse extracts resources out of class jar files into temp directory; but doesn't extract class files from the jar or permit jar url input source, as far as I have discovered. Second, even if this could be solve, is it wise to put a dependency on eclipse into EJB? -1 in my opinion.

Regards,
John


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 24, 2005 3:06 pm 
Newbie

Joined: Thu Nov 18, 2004 6:01 pm
Posts: 17
Allow me to be the first to point out that I am wrong to cast doubt on the efficacy of asLocalURL. I think I misinterpret the javadoc on rg.eclipse.core.runtime.Platform.resolve(url) and spread that over to asLocalURL. Sorry. This afternoon, I think the javadoc is only warning the caller of resolve and asLocalURL to be prepared to handle jar: and http: url's as well as file: url.

Regards,
John


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.