-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problems with Persistence.xml not on class path
PostPosted: Sun Jul 15, 2007 2:56 am 
Newbie

Joined: Mon May 28, 2007 4:20 am
Posts: 6
I am using Hibernate Entity Manager 3.31GA, annotations 3.30GA and core 3.2.

I have an application with a 'plugin' system. The plugins are in JARs in a subdirectory. The main application searches the JARs for classes implementing a particular interface, and loads those using URLClassLoader.
The problem arises when one of these plugins tries to use the Entity Manager. The plugin JAR has a persistence.xml file, but the system class loaders don't seem to 'know' about this JAR because it isn't on the class path.

The usual thing is to get the following:

Code:
INFO  - Hibernate EntityManager 3.3.1.GA
DEBUG - Look up for persistence unit: WhateverPU
INFO  - Could not find any META-INF/persistence.xml file in the classpath
java.util.concurrent.ExecutionException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named WhateverPU
etc...


I also tried using Ejb3Configuration (out of desperation because it's not portable) - but here I suffer from lack of documentation. Can I somehow give a URL to the persistence.xml in the JAR file? If I try to specify everything else programmatically, without referring to the persistence.xml, Hibernate seems to expect .hbm.xml files for classes that are actually annotated:



Code:
                  Ejb3Configuration ejbc = new Ejb3Configuration();
  return ejbc.addClass(Cards.class)
                                 .addClass(Places.class)
                                       .addProperties(map)
                                       .buildEntityManagerFactory();
                 


And this produces:

Code:
INFO  - Reading mappings from resource: com/tvg_tech/holypass/general/Cards.hbm.xml
INFO  - Reading mappings from resource: com/tvg_tech/holypass/general/Cards.hbm.xml
java.util.concurrent.ExecutionException: org.hibernate.MappingNotFoundException: resource: com/xxx/xxx/general/Cards.hbm.xml not found


Many thanks,
Shalom Crown


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 3:34 am 
Newbie

Joined: Mon May 28, 2007 4:20 am
Posts: 6
Well I didn't see the Ejb3Configuration method addAnnotatedClass - which solves that problem - but I think there is another similar one. Even though the Entity manager starts OK:


Code:
INFO  - Binding entity from annotated class: com.xxx.xxx.general.Cards
DEBUG - Binding column DTYPE unique false
DEBUG - Import with entity name=Cards
INFO  - Bind entity com.xxx.xxx.general.Cards on table Cards
DEBUG - Processing com.txxx.xxx.general.Cards property annotation
DEBUG - Processing com.xxx.xxx.general.Cards field annotation
DEBUG - Processing annotations of com.xxx.xxx.general.Cards.id
DEBUG - Binding column id unique false
DEBUG - id is an id
DEBUG - building SimpleValue for id
DEBUG - Building property id
DEBUG - Cascading id with null
DEBUG - Bind @Id on id
DEBUG - Processing annotations of com.xxx.xxx.general.Cards.GUID
DEBUG - Binding column GUID unique false
DEBUG - binding property GUID with lazy=false
DEBUG - building SimpleValue for GUID
DEBUG - Building property GUID
DEBUG - Cascading GUID with null

Etc.

Soon, it runs into problems - again, to do with the class loader not being able to find the class in the plugin JAR:

Code:
WARN  - Unable to apply constraints on DDL for com.xxx.xxx.general.Cards
java.lang.ClassNotFoundException: com.xxx.xxx.general.Cards
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at org.hibernate.annotations.common.util.ReflectHelper.classForName(ReflectHelper.java:78)
        at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.classForName(JavaReflectionManager.java:74)
        at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:158)
etc..


I would still prefer to use the standard JPA API, rather than Hibernate's specific things.

There is a persistence.xml tag called jpa-file - which allows you to specify a URL to the persistence.xml. If I could add this information programmatically, it would save a lot of problems.
- Shalom Crown


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 3:11 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you set the ThreadLocal classloader that sees the persistence.xml and the entity classes before calling Persistence.create... you should be fine I guess.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 2:24 am 
Newbie

Joined: Mon May 28, 2007 4:20 am
Posts: 6
I don't understand what you mean by 'ThreadLocal classloader'. Is there some ThreadLocal variable that the Persistence class uses?

I also tried adding the plugin JAR to and Ejb3Configuration using
Code:
.addResource(jarPath, this.getClass().getClassLoader())

Where 'jarPath' is the absolute path of the plugin JAR and 'this' is the plugin class - but this failed with "resource ..... .jar not found".

In the end I will have to ask the plugin developer to add his JAR file to the class path (say - on the command line that starts the application). This is ugly, but not unreasonable.

- Shalom Crown


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 1:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
Thread.currentThread().setContextClassLoader();

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 2:27 pm 
Newbie

Joined: Mon May 28, 2007 4:20 am
Posts: 6
Well - that certainly works!

Many thanks!

- Shalom Crown


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.