严重:初始SessionFactory创建失败.java.lang.NoSuchMethodError:org.objectweb.asm.ClassWriter。<init>(Z)V </init>

时间:2012-07-06 10:53:52

标签: hibernate

我正在使用netbeans, 并希望将struts和hibernate集成到一个简单的应用程序中,以在jsp页面上显示表的所有数据。

但是我收到以下错误

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.reflect.InvocationTargetException

在glassfish服务器的日志中出现以下错误

    SEVERE: Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
SEVERE: Initial SessionFactory creation failed.java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V

HibernateUtil.java:

package controller;

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author ROMO
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

FindQuestion.java:

package controller;

import beans.Question;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

/*
 * @author ROMO
 */
public class FindQuestion extends ActionSupport {

    private String q = null;
    private String categoryname = null;
    private List questionList = new ArrayList();

    @Override
    public String execute() throws Exception {
        q = "Question";
        categoryname = "Category";
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            this.questionList = (List<Question>) session.createQuery("from Question").list();
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

}

有人可以帮助我。

3 个答案:

答案 0 :(得分:3)

您的 asm 版本与您的Hibernate版本不兼容。检查兼容版本的一种方法是查看maven依赖项,例如hibernate-core。即使你没有maven构建,它也是可用的方式。

例如,一个工作组合是:

  • Hibernate 3.6.10,
  • cglib 2.2
  • asm 3.1。

如果您需要更适合您的答案,您必须分享目前包含哪些库的信息。

答案 1 :(得分:1)

我发现了这个:

http://www.hildeberto.com/2008/05/hibernate-and-jersey-conflict-on.html

基本上,以防上述完整说明如下: 我通过使用cglib-nodep-2.2.3.jar修复了asm的类加载问题。这需要替换的原因是ASM是一个受欢迎的库。 Hibernate是针对asm 1.5.3构建的,但是asm现在是版本4.在某个地方,我的容器中的类加载器正在拾取更新版本的asm,类ClassWriter已被更改。 Hibernate在封面下使用cglib,后者又使用asm 1.5.3。这是问题所在。构建cglib-nodep是为了解决此问题。 asm类已从其标准包移至cglib包,并且这些是nodep库使用的cglib版本。

Spring采用了非常相似的方法。

答案 2 :(得分:1)

只需添加到您的pom.xml:

<dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-asm</artifactId>
        <version>1.0</version>
</dependency>
相关问题