-->
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.  [ 11 posts ] 
Author Message
 Post subject: Could not initialize class...HibernateUtil.java
PostPosted: Mon Dec 18, 2006 2:47 am 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
I am using Hibernate 3.2.1GA and Annotations 3.2.1GA.

Before i was using hbm.xml files and everything was working fine, but since I've switch to using annotations (and deleting my hbm.xml files) my HibernateUtil.java class throws a:

java.lang.NoClassDefFoundError: Could not initialize class

I've been trying to figure out why this is happening, but i cannot figure it out. Here is my Hibernate Util...





Code:
public class HibernateUtil {
   
   private static final SessionFactory sessionFactory;
   
   static{
      try{
         AnnotationConfiguration cfg = new AnnotationConfiguration();
         cfg.configure("hibernate.cfg.xml");
         cfg.setPrecedence("class");
         sessionFactory = cfg.buildSessionFactory();
      }catch(Throwable hb){
         throw new ExceptionInInitializerError(hb);
      }
   }
   
   private static final ThreadLocal <Session>localSession = new ThreadLocal<Session>();
   
   public static Session currentSession(){
      /*Session s = (Session)localSession.get();
      
      if(s == null){
         s = sessionFactory.openSession();
         localSession.set(s);
      }
      */
      return sessionFactory.openSession();
   }
   
   public static void closeSession(Session session)throws HibernateException{
      session = localSession.get();
      
      if(session != null){
         session.close();
         localSession.set(null);
      }
   }

}


btw, i'm using JEE5 and jdk6


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 12:24 pm 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
anyone have any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
dunno, you forgot some jars?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 3:52 pm 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
I don't think so.

It was working fine using hbm.xml files.

The only thing i did was modify that hibernateUtil, delete the hbm.xml files, added the two annotation jar files required from the hibernate-annotation download. And of course modified my hibernate.cfg.xml and persistent classes.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 9:27 pm 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
i've even tried downgrading to jdk1.5, nothing changed.

tried clearing build folder, copied the hibernateuUttil exactly from the annotations tutorial, and still nothing.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 1:50 pm 
Newbie

Joined: Fri Dec 15, 2006 11:09 am
Posts: 10
If I understand your post correctly, you did not specify which class it can not find. Which class?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 10:16 pm 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
it appears it cannot find the class that needs to initialize.. my HibernateUtil class.

But it seems like an error where a .jar file cannot be found.


All the hibernate 3.2.1 jar files including hibernate3.jar are in my lib, as well as hibernate-annotations.jar and ejb3-persistence.jar

It seems like the hibernate-annotations.jar is the cause of the problem, because everything was working before that file was moved into my lib.

I have tried pretty much everything i know to try, and nothing has worked...i've even tried using my previous install of Sun App Server that used Jdk 1.5.

I'll give it a couple more days, then if all else fails ... just go back to using hbm.xml files


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 10:58 am 
Newbie

Joined: Fri Dec 15, 2006 11:09 am
Posts: 10
If it is the HibernateUtil is the class def not found, I do not think the problem is the Annotations jar. In my code, the initial call to any class in the Annotation jar is in the HibernateUtil (the new AnnotationConfiguration), so if the jvm does not find the HibernateUtil class, it does not ever get to a call on a class in the Annotation jar.

One thing I noticed about your methodology, you described that you went "all or nothing". I would approach this more incrementally, make a smaller number of changes at a time, so that you have a better chance to troubleshoot what is wrong.

My recommendation:

1. Get it working the way it was pre annotation.
a. Put it back the way it was (pre annotation attempt).
b. Test to confirm it works without the class def not found error.

2. Get the annotation jar dependency working.
a. add the jars needed by Annotations to your classpath.
b. in your HibernateUtil class, revise the line that news up the Configuration to new up an AnnotationConfiguration instead.
c. Test to confirm it works.

3. Then change only one hbm described class to being an annotated class.
a. Annotate one of your pojos.
b. Revise your hibernate.cfg.xml to comment out the line that is the mapping resource reference to the hbm file for that one pojo.
c. Revise your hibernate.cfg.xml to add a line - a mapping class reference to your newly annotated pojo.
d. Test to make sure this works.

Then you can add any number of other classes after, in the same manner as 3.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 11:53 am 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
That sounds like a good idea, i'm almost all the way complete on getting it back to pre-annotations working condition.

This is a really strange problem, but i feel it's probably something simple and/or silly.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 3:12 am 
Newbie

Joined: Mon Dec 18, 2006 2:39 am
Posts: 16
Hmmm i fixed it...i had to add system.out everywhere, and it actually said there was an association error lol


It still doesn't explain why it didn't work when i created the new project with only one pojo and no association. Or why a working set failed with annotations. I'm tempted to try annotations again :)


Top
 Profile  
 
 Post subject: jar missing
PostPosted: Wed Mar 04, 2009 12:04 am 
Newbie

Joined: Tue Mar 03, 2009 10:55 pm
Posts: 1
Hi all,

I had the same error -
and after some debugging (and catching also _throwables_ - and not just exceptions) I found out that jars for the slf4j were missing.

The detailed solution is here:
http://liferay-portlets.blogspot.com/20 ... class.html

Hope it helps,

Simon

_________________
Blog on liferay portlets in java - with hibernate


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