我是hebirnate的新人。尝试执行示例代码。
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();
}
}
但是获得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
我做错了什么?
更新
在classpath中添加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
答案 0 :(得分:4)
如果有的话,你需要在类路径中添加apache commons logging jar。否则从apache下载它并将其添加到项目中并将其添加到项目的classpth中。
<强>更新强>
下载dom4j库here并将其添加到类路径中。
<强> UPDATE2 强>
信息:Hibernate 3.0.3 - &gt; INFO LOG显示您正在使用的hibernate版本。
INFO:找不到hibernate.properties - &gt;信息日志显示hibernate.properties
未找到。这是提供hibernate属性的另一种方法,您可以在hibernate.cfg.xml
中提供这些属性。
有关详情,请查看here
答案 1 :(得分:1)
如果您只是为了尝试hibernate而制作一个示例项目,那么Nandkumar建议的就是好的,只需继续添加NoClassDefFoundError
中报告的类的jar。
这个过程非常繁琐,理想情况下你应该使用像maven这样的依赖管理工具来解决Hibernate(或任何其他类似框架)的所有依赖关系。此线程可以帮助您设置maven以使用休眠。它也可以帮助你移动hibernate的版本