web-dev-qa-db-ja.com

Hibernate NoSuchFieldError INSTANCE、ただしStruts 1のみ?

私は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で同じことを実行しようとすると、例外が発生します。これはNOT WORKが行うことです:

_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;
  }
}
_

HibernateUtilConfiguration config = new Configuration()行は、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)
_

何が悪いのですか?

ありがとう!

19
cbmeeks

クラスパスにHibernate jarのいくつかのバージョンがあるようです。

21
axtavt

これらの2つの依存関係を削除するとうまくいきました。

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.0.0.ga</version>
        <scope>runtime</scope>
    </dependency>
     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency> 

これらは私の休止状態の依存関係です。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.3.Final</version>
</dependency>
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.17.1-GA</version>
</dependency>
<dependency>
    <groupId>asm</groupId>
    <artifactId>asm-all</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>antlr</groupId>
    <artifactId>antlr</artifactId>
    <version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.3.Final</version>
</dependency>
1
Seeker

@axtavtは正しいと言います。あなたはすでに問題を解決していると思います。他の人に役立つかもしれないので、コメントを投稿します。

Mavenの依存関係を使用している場合は、独自の休止状態の依存関係がすでにあるとします。その後、別の依存関係(org.codehaus.jacksonやdrools-springなど)がpomに追加されます。 org.codehaus.jacksonとdrools-springが異なるバージョンの休止状態に独自の依存関係を持っているため、このエラーが発生する可能性があります。 org.codehaus.jacksonを追加するときにhibernateを除外するか、drools-springが問題を解決します。

0
Shasha.Zhu