简体   繁体   中英

Hibernate's Session exception

Im new in hebirnate. Try execute example code.

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (HibernateException ex) {
        throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
    }
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
    Session s = (Session) session.get();
    // Open a new Session, if this Thread has none yet
    if (s == null) {
        s = sessionFactory.openSession();
        session.set(s);
    }
    return s;
}

public static void closeSession() throws HibernateException {
    Session s = (Session) session.get();
    session.set(null);
    if (s != null)
        s.close();
}
}


public class javism_jaxb_xml_to_object {
public static void main(String[] args) throws JAXBException
{
    Session session = HibernateUtil.currentSession();
  Transaction tx = null;
    tx = session.beginTransaction();
            javism_jaxb cust = new javism_jaxb();
            cust.setName("Vasay");
            cust.setId(1);
            cust.setAge(34);
            session.save(cust);
            tx.commit();
}
}

But get exeption

  Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:111)
at test.HibernateUtil.<clinit>(HibernateUtil.java:16)
at test.javism_jaxb_xml_to_object.main(javism_jaxb_xml_to_object.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 3 more

What im doing wrong?

UPDATE

After adding commons-logging-1.1.1.jar in classpath. And refreshing hibernate3.jar from 3.0.3 to 3.5 i get another exeption

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at test.HibernateUtil.<clinit>(HibernateUtil.java:16)
at test.javism_jaxb_xml_to_object.main(javism_jaxb_xml_to_object.java:25)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 2 more

but if i use hibernate3.jar version 3.0.3 i get this

15.08.2012 11:01:30 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.0.3
15.08.2012 11:01:30 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
15.08.2012 11:01:30 org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
15.08.2012 11:01:30 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
15.08.2012 11:01:30 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
15.08.2012 11:01:30 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml

You need to add apache commons logging jar in your classpath if you have it. Otherwise download it from apache and add it in your project and add it in project's classpth.

UPDATE

download dom4j library here and add it in classpath.

UPDATE2

INFO: Hibernate 3.0.3 --> INFO LOG SHOWING hibernate version you are using.

INFO: hibernate.properties not found --> INFO LOG SHOWING hibernate.properties not found. This is an alternate way to give hibernate properties, you do provide these properties in hibernate.cfg.xml .

For more info check here

If you are just making a sample project for trying out hibernate, then what Nandkumar suggested is ok, just keep adding jars of the classes that are reported in NoClassDefFoundError .

This process is very tedious, ideally you should use a dependency management tool like maven which can resolve all the dependencies of Hibernate (or any other such framework). This thread can help you setup maven for using hibernate. It will also help you while moving across versions of hibernate as well

Adding Hibernate 3.5.x to a maven pom.xml build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM