-->
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: entity class not found: Configuration.addJar() problem
PostPosted: Tue May 29, 2007 11:38 am 
Newbie

Joined: Thu May 24, 2007 6:08 am
Posts: 4
Hi,
I'm an Italian Engineer and i with my team are developing an Application (wtih new framework Echo2) and we choose Hibernate 3.

The problem is very serious and we need a solution:

I explain:

1 - the file hibernate.cfg.xml is located in the direcotory C:\test\ (under WindowsXP) and I can load it without problem in this way:

private static org.hibernate.SessionFactory SESSION_FACTORY = new org.hibernate.cfg.Configuration().configure(new File(CONFIG_FILE_LOCATION_FILE));

( CONFIG_FILE_LOCATION_FILE = C:/test/hibernate.cfg.xml )

2 - the file testing.jar (inside of which there are .hbm.xml and .class files) is located in C:\test\ too, therefore I need the file C:\test\testing.jar

3 - THE PROBLEM: when I load the file testing.jar file the classes are not found!! I use this code:

SESSION_FACTORY = new org.hibernate.cfg.Configuration().configure(new File(CONFIG_FILE_LOCATION_FILE)).addJar(new File("C:/test/testing.jar")).buildSessionFactory();


We have tried to load dinamically at runtime the class of persistence objects with URLCLassLoader but something goes wrong!

I hope of to have been clear, thanks in advance.
We need some help, thanks very much.


Regards

Hibernate version:3.1


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:05 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I think that addJar method only reads in the .hbm.xml files to the Configuration class from that Jar file. I'm pretty sure you still need to add the jar to your program's classpath if the related .class files are in there.

And, make sure your program can access that C:/test directory to read them.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:22 pm 
Newbie

Joined: Thu May 24, 2007 6:08 am
Posts: 4
Hi,
Thanks really for the response, You have centered the problem perfectly!

I've tried to load only hbm.xml files (which are in a jar external of classpath) with .class (of persistence object in the CLASSPATH) ---> it works very well!

But my problem is that the .class are external.

You wrote right, the file must be on the CLASSPATH!...but in this application I've already call a file.class which is external of classpath (with an extensions of URLClassLoader) and it works very well.

Therefore I think: why Hibernate cannot find the .class, solution must exist one!! .. I hope...

Perhaps Hibernate Annotations could be a solution?.. EntityManager?

I hope..

Thanks in advance.

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:34 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
so, are the .class files that are referenced in the .hbm.xml files loaded before you run that Configuration set up?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:37 pm 
Newbie

Joined: Thu May 24, 2007 6:08 am
Posts: 4
Yes!

I load the class before...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 12:50 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I don't know then why it wouldn't pick those up. See if you can use EntityManager to manually load them.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 6:01 am 
Newbie

Joined: Thu May 24, 2007 6:08 am
Posts: 4
Hi,
I think I've solved the problem in a way that I don't like...


Code:
File baseFile = new File(baseFileName);
// Then, load additional libraries into SYSTEM CLASSLOADER'S CLASSPATH
      String classPathString = baseFile.getParent();
      File oooClassPath = new File(classPathString);
      File[] files = oooClassPath.listFiles();
      int count = 0;
      int i;
      for (i = 0; i < files.length; i++) {
         try {
            String name = files[i].getAbsolutePath().toUpperCase();
            if (name.endsWith(".JAR") && !name.equalsIgnoreCase(baseFileName)) {
               ClassPathHacker.addFile(files[i]);
               System.out.println(files[i].getAbsolutePath());
            }
         } catch (MalformedURLException ex) {
            System.out
                  .println("ClassManager.getURLClassLoader - in for loop, MalformedURLException: "
                        + ex.getMessage());
         } catch (IOException ex) {
            System.out
                  .println("ClassManager.getURLClassLoader - in for loop, IOException, param: "
                        + files[i].getAbsolutePath());
         }
      }
      return (URLClassLoader) ClassLoader.getSystemClassLoader();


..and ClassPathHacker is (I found it in Sun's website):

Code:
public class ClassPathHacker {

   private static final Class[] parameters = new Class[] { URL.class };

   public static void addFile(String s) throws IOException {
      File f = new File(s);
      addFile(f);
   }// end method

   public static void addFile(File f) throws IOException {
      addURL(f.toURL());
   }// end method

   public static void addURL(URL u) throws IOException {

      Class sysclass = URLClassLoader.class;
      URLClassLoader sysloader = (URLClassLoader) ClassLoader
            .getSystemClassLoader();

      try {
         Method method = sysclass.getDeclaredMethod("addURL", parameters);
         method.setAccessible(true);
         method.invoke(sysloader, new Object[] { u });
      } catch (Throwable t) {
         t.printStackTrace();
         throw new IOException(
               "Error, could not add URL to system classloader");
      }// end try catch

   }// end method

}// end class



Now I have to test it..and so I'll tell you about..

Thanks!

Regards


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.