java hibernate struts

java - Hibernar NoSuchFieldError INSTANCE pero solo con Struts 1?



hibernate (3)

@axtavt dice correcto. Sé que ya resolviste el problema, publico mis comentarios, ya que podría ser útil para otras personas.

Si está utilizando dependencias de Maven, suponga que ya tiene su propia dependencia de hibernación. Y más tarde, otra dependencia (como org.codehaus.jackson o drools-spring) se agrega al pom. Podría causar este error porque org.codehaus.jackson y drools-spring tienen su propia dependencia en una versión diferente de hibernación. excluya hibernación cuando agregue org.codehaus.jackson o drools-spring resolvería el problema.

Soy nuevo en Java e Hibernate (siendo desarrollador de Rails y C #). De todos modos, tengo un programa de prueba que funciona bien con Hibernate pero mi aplicación web real (Struts 1) se bloquea con:

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

Estoy usando:

Hibernate 3.6.1 with Annotations

Struts 1 with Apache Tiles

C3P0 connection pool

Aquí está el programa que realmente FUNCIONA . Es parte del proyecto principal. Sólo una simple prueba de 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() ); } }

Funciona bien Muestra un poco de datos de muestra.

Ahora, cuando trato de hacer lo mismo en Struts, obtengo la excepción. Esto es lo que NO FUNCIONA :

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

Esta es mi clase de 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; } }

La Configuration config = new Configuration() en el HibernateUtil es la línea que se bloquea en Struts pero no la aplicación de prueba.

Aquí está el volcado de excepción completo:

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)

¿Qué estoy haciendo mal?

¡Gracias!


Parece que tienes varias versiones de jars de Hibernate en el classpath.


Eliminar estos dos dependencias funcionó para mí.

<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>

Estas son mis dependencias de hibernación.

<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>