請問各位高手,我出現了一個令我頭痛的問題:在HibernateUtil類中建立SessionFactory總是失敗:

來源:互聯網
上載者:User

 請問各位高手,我出現了一個令我頭痛的問題:在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.ExceptionInInitializerErrorcom.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/Signaturejava.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)
很急啊,請各為高手幫我解決一下?為什麼啊?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.