当前位置: 首页 > 面试题库 >

Hibernate NoSuchFieldError实例但仅使用Struts 1?

竺国兴
2023-03-14
问题内容

我是Java和Hibernate的新手(是Rails和C#开发人员)。无论如何,我有一个可以在Hibernate上正常运行的测试程序,但是我的实际Web应用程序(Struts 1)崩溃了:

SEVERE: Servlet.service() for servlet default threw exception java.lang.NoSuchFieldError: INSTANCE

我在用:

Hibernate 3.6.1 with Annotations

Struts 1 with Apache Tiles

C3P0 connection pool

下面是实际的程序WORKS。这是主体项目的一部分。只是一个简单的JAVA测试。

public class TestUser {

  public static void main(String[] args) {

    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    String queryString = "from User where UserName = 'Quake'";
    Query query = session.createQuery(queryString);

    User quake = (User)query.uniqueResult();
    session.getTransaction().commit();

    System.out.println( quake.getEmail() );
    System.out.println( "Active? " + quake.isActive() );


    UserRepository userRepo = new UserRepository();

    System.out.println( "User Quake: " + userRepo.findAll().get(0).getEmail() );

  }
}

效果很好。输出一些样本数据。

现在,当我尝试在Struts中执行相同的操作时,出现了异常。这是行不通的:

public class ListUsers extends org.apache.struts.action.Action {

  /* forward name="success" path="" */
  private static final String SUCCESS = "success";

/**
 * This is the action called from the Struts framework.
 * @param mapping The ActionMapping used to select this instance.
 * @param form The optional ActionForm bean for this request.
 * @param request The HTTP Request we are processing.
 * @param response The HTTP Response we are processing.
 * @throws java.lang.Exception
 * @return
 */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {


    List<String> listMsg = new ArrayList<String>();

    listMsg.add("Message A");
    listMsg.add("Message B");
    listMsg.add("Message C");
    listMsg.add("Message D");

    request.setAttribute("listMsg", listMsg);



    UserRepository userRepo = new UserRepository();  // CRASHES HERE (see HibernateUtil)
    String email = userRepo.first().getEmail();


    return mapping.findForward(SUCCESS);
  }
}

这是我的HibernateUtil类:

public class HibernateUtil {

  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        Configuration config = new Configuration();  // THIS LINE CRASHES IN STRUTS

        // Add annotated classes
        config.addAnnotatedClass(User.class);

        config.configure();
        return config.buildSessionFactory();

    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed.  WTFQNIL: " + ex);
        throw new ExceptionInInitializerError(ex);
    }
  }

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

Configuration config = new Configuration()HibernateUtil是在Struts中崩溃的行,但不是测试应用程序中崩溃的行。

这是完整的异常转储:

Feb 24, 2011 9:51:58 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
java.lang.NoSuchFieldError: INSTANCE
at org.hibernate.type.BasicTypeRegistry.<init>(BasicTypeRegistry.java:94)
at org.hibernate.type.TypeResolver.<init>(TypeResolver.java:59)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:249)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:300)
at com.kencogroup.kkms.struts.utilities.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
at com.kencogroup.kkms.struts.utilities.HibernateUtil.<clinit>(HibernateUtil.java:17)
at com.kencogroup.dao.repositories.UserRepository.first(UserRepository.java:33)
at com.kencogroup.kkms.struts.actions.ListUsers.execute(ListUsers.java:52)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:662)

我究竟做错了什么?


问题答案:

看起来您在类路径中有多个版本的Hibernate jar。



 类似资料:
  • 问题内容: 当尝试运行我的Android应用程序时出现此错误,而我根本不使用Kotlin,该应用程序是用Java编写的… 问题答案: 我这样解决了: 删除所有.gradle文件夹 无效的缓存/重启一切对我来说都可以。

  • 我已经容器化了一个Spring引导应用程序。它包含在一个jar文件中。当我单独调用jar文件时,它正在工作,但是当我通过运行它时,它会产生以下错误: ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“crawle

  • 我们看到Akka被成功运用在众多行业的众多大企业,从投资业到商业银行、从零售业到社会媒体、仿真、游戏和博彩、汽车和交通系统、医疗保健、数据分析等等等等。对任何需要高吞吐率和低延迟的系统,Akka都是优秀的候选。 这里是一个由生产用户撰写的关于Akka的使用实例的非常好的讨论 以下是Akka被部署到生产环境中的领域 事务处理 (在线游戏,金融/银行业,贸易,统计,赌博,社会媒体,电信) 垂直扩展,水

  • 问题内容: 我有一个JTextArea,它只需要接受数字。这是我的代码: 对正数有效,但对负数无效。我该如何解决? 编辑:抱歉,在Internet上找到了AxisJTextFilter,而我忘记了这一点。它的代码是: 问题答案: 尝试修改正则表达式(int验证方法containsOnlyNumbers)。 这将接受以下数字: 1234 -1234 +1234 1234.1234 希望对您有所帮助

  • 本文向大家介绍PHP curl使用实例,包括了PHP curl使用实例的使用技巧和注意事项,需要的朋友参考一下 概述 本博客的前面两篇文章:curl和libcurl简介以及PHP中使用curl对PHP中curl的使用做了简单介绍,但是PHP中curl的使用却并不简单,尤其是curl的各种配置项,本篇文章会讲解几个PHP的实例,以便大家更好的理解curl。 实例:抓取页面 使用curl抓取页面相对来