请问各位高手,我出现了一个令我头痛的问题:在HibernateUtil类中创建SessionFactory总是失败:
package com.zel.dao.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
System.out.println("Initial SessionFactory creation failed.");
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal tLocalsess = new ThreadLocal();
public static final ThreadLocal tLocaltx = new ThreadLocal();
/*
* getting the thread-safe session for using
*/
public static Session currentSession() {
Session session = (Session) tLocalsess.get();
// open a new one, if none can be found.
try {
if (session == null || !session.isOpen()) {
session = openSession();
tLocalsess.set(session);
}
} catch (HibernateException e) {
// throw new HibernateException(e);
e.printStackTrace();
}
return session;
}
/*
* closing the thread-safe session
*/
public static void closeSession() {
Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try {
if (session != null && session.isOpen()) {
session.close();
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
/*
* begin the transaction
*/
public static void beginTransaction() {
System.out.println("begin tx");
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx == null) {
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
/*
* close the transaction
*/
public static void commitTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
tLocaltx.set(null);
System.out.println("commit tx");
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
/*
* for rollbacking
*/
public static void rollbackTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
private static Session openSession() throws HibernateException {
return getSessionFactory().openSession();
}
private static SessionFactory getSessionFactory() throws HibernateException {
return sessionFactory;// SingletonSessionFactory.getInstance();
}
}
在Tomcat服务器报下面的错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError com.zel.dao.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:20) com.zel.dao.imp.SpecialtyDAOImp.saveSpecialty(SpecialtyDAOImp.java:83) com.zel.web.struts.action.AddSpecialtyAction.execute(AddSpecialtyAction.java:55) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) javax.servlet.http.HttpServlet.service(HttpServlet.java:710) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NoClassDefFoundError: org/aspectj/lang/Signature java.lang.Class.forName0(Native Method) java.lang.Class.forName(Class.java:169) org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:57) org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124) org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56) org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:366) org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:60) org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881) org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1174) com.zel.dao.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:16) com.zel.dao.imp.SpecialtyDAOImp.saveSpecialty(SpecialtyDAOImp.java:83) com.zel.web.struts.action.AddSpecialtyAction.execute(AddSpecialtyAction.java:55) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) javax.servlet.http.HttpServlet.service(HttpServlet.java:710) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
很急啊,请各为高手帮我解决一下?为什么啊?