【问题标题】:Hibernate's Session exceptionHibernate 的 Session 异常
【发布时间】:2012-08-11 10:47:48
【问题描述】:

我是赫伯纳特的新手。尝试执行示例代码。

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();
}
}

但得到例外

  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

我做错了什么?

更新

在类路径中添加 commons-logging-1.1.1.jar 之后。并将 hibernate3.jar 从 3.0.3 刷新到 3.5 我得到另一个异常

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

但如果我使用 hibernate3.jar 版本 3.0.3 我会得到这个

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

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    如果你有的话,你需要在你的类路径中添加 apache commons logging jar。否则从apache 下载它并将其添加到您的项目中并添加到项目的classpth中。

    更新

    下载 dom4j 库here 并将其添加到类路径中。

    更新2

    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。这是提供休眠属性的另一种方法,您确实在hibernate.cfg.xml 中提供这些属性。

    更多信息请查看here

    【讨论】:

    • @KliverMax:总是乐于提供帮助:)
    • 对不起,它没有帮助。我得到另一个例外。请看更新
    • 现在正在添加百万库。我希望它快点结束。
    • Rrrrrr stakoverflow 没有给我编辑问题,所以我在这里提出了新问题。新的例外。Caused by: java.lang.IllegalStateException: org.slf4j.LoggerFactory could not be successfully initialized. See also http://www.slf4j.org/codes.html#unsuccessfulInit。也许您可以在任何地方上传hibernate工作所需的所有库?
    • 我将版本从 3.0.3 更改为 3.5。但我无法编辑问题((
    【解决方案2】:

    如果您只是制作一个示例项目来尝试休眠,那么 Nandkumar 的建议是可以的,只需继续添加 NoClassDefFoundError 中报告的类的 jar。

    这个过程非常繁琐,理想情况下你应该使用像maven这样的依赖管理工具,它可以解决Hibernate(或任何其他此类框架)的所有依赖关系。该线程可以帮助您设置 maven 以使用 hibernate。它还可以帮助您在不同版本的 hibernate 之间移动

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-09
      • 2013-04-05
      • 2016-03-23
      • 1970-01-01
      • 2019-12-07
      • 2011-08-31
      • 2019-09-27
      • 1970-01-01
      相关资源
      最近更新 更多