我是新来的。尝试执行示例代码。
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