【发布时间】:2010-11-08 00:31:54
【问题描述】:
我正在尝试开始使用 Hibernate,但在执行我的程序时,我在初始化期间遇到错误。 异常是这个类抛出的,复制自here:
package net.always_data.bastien_leonard;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
这是堆栈跟踪:
> java net/always_data/bastien_leonard/Main
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.always_data.bastien_leonard.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at net.always_data.bastien_leonard.HibernateUtil.<clinit>(HibernateUtil.java:8)
at net.always_data.bastien_leonard.Main.main(Main.java:17)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at net.always_data.bastien_leonard.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 2 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 3 more
我不知道问题出在哪里,所以我真的不知道在哪里看:
- 安装问题?这是由 Maven 处理的,所以我猜它是正确的。
- Hibernate 找不到配置文件?
- 类路径问题?
我正在从包含我的 hibernate.cfg.xml 文件的类路径的根目录调用程序。 以下是它在实践中的样子:
> pwd
/home/bastien/info/java/hibernate/test/Test/target/classes
> echo $CLASSPATH
/home/bastien/info/java/hibernate/test/Test/target/classes
> ls -F
hibernate.cfg.xml net/
> ls -FR
.:
hibernate.cfg.xml net/
./net:
always_data/
./net/always_data:
bastien_leonard/
./net/always_data/bastien_leonard:
Event.class Event.hbm.xml HibernateUtil.class Main.class
我尝试查看 Hibernate 提供的教程示例,但 Maven 无法编译它们;它抱怨缺少工件。
顺便说一句,Maven 只允许我使用 Hibernate 3.3.1。是否可以使用 3.3.2 并且仍然让 Maven 处理安装?
【问题讨论】:
标签: java hibernate configuration persistence