Hibernate NoSuchFieldError 实例但仅适用于 Struts 1?
我是 Java 和 Hibernate 新手(作为 Rails 和 C# 开发人员)。无论如何,我有一个测试程序可以与 Hibernate 一起正常工作,但我的实际 Web 应用程序 (Struts 1) 崩溃:
SEVERE: Servlet.service() for servlet default throws exception java.lang.NoSuchFieldError: INSTANCE
我正在使用:
Hibernate 3.6.1 with Annotations
Struts 1 with Apache Tiles
C3P0 连接池
这是实际上有效的程序。这是主要项目的一部分。只是一个简单的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;
}
}
HibernateUtil
中的 Configuration 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)
我做错了什么?
谢谢!
I'm new to Java and Hibernate (being a Rails and C# developer). Anyway, I have a test program that works fine with Hibernate but my actual web app (Struts 1) crashes with:
SEVERE: Servlet.service() for servlet default threw exception
java.lang.NoSuchFieldError: INSTANCE
I am using:
Hibernate 3.6.1 with Annotations
Struts 1 with Apache Tiles
C3P0 connection pool
Here is the program that actually WORKS. It's a part of the main project. Just a simple JAVA test.
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() );
}
}
Works just fine. Outputs a little sample data.
Now, when I try to do the same thing in Struts, I get the exception. This is what does 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);
}
}
This is my HibernateUtil class:
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;
}
}
The Configuration config = new Configuration()
line in the HibernateUtil
is the line that crashes in Struts but not the test app.
Here is the complete exception dump:
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)
What am I doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来类路径中有多个版本的 Hibernate jar。
It looks like you have several versions of Hibernate jars in the classpath.
删除这两个依赖项对我有用。
这些是我的休眠依赖项。
Removing these two dependency worked for me.
These are my hibernate dependencies.
@axtavt 说得对。
我知道您已经解决了问题,我发布我的评论,因为它可能对其他人有帮助。
如果您正在使用 Maven 依赖项,则假设您已经拥有自己的 Hibernate 依赖项。后来,另一个依赖项(如 org.codehaus.jackson 或 drools-spring)被添加到 pom 中。它可能会导致此错误,因为 org.codehaus.jackson 和 drools-spring 对不同版本的 hibernate 有自己的依赖。添加 org.codehaus.jackson 或 drools-spring 时排除 hibernate 可以解决问题。
@axtavt says right.
I know you already solved the problem, I post my comments as it might be helpful to other people.
If you are using maven dependencies, say you already have your own hibernate dependency. And later, another dependency (like org.codehaus.jackson or drools-spring) is added to the pom. It might cause this error because org.codehaus.jackson and drools-spring have their own dependency on a different version hibernate. exclude hibernate when you add org.codehaus.jackson or drools-spring would solve the prob.