1

我正在使用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;
    }
}

查找问题.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;
    }

}

有人可以请帮帮我。

4

3 回答 3

3

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

例如,一种工作组合是:

  • 休眠 3.6.10,
  • cglib 2.2
  • 汇编 3.1。

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

于 2012-07-06T11:35:24.210 回答
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 采用了非常相似的方法。

于 2013-05-31T14:14:54.710 回答
1

只需添加到您的 pom.xml :

<dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-asm</artifactId>
        <version>1.0</version>
</dependency>
于 2015-05-29T20:14:25.980 回答