开发者

Spring & hibernate configuration (using maven): java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration

开发者 https://www.devze.com 2022-12-28 06:00 出处:网络
I am trying to include spring and hibernate in an application running on a Weblogic 10.3 server. When I run the application in the server, while accessing an TestServlet to check my configuration I ge

I am trying to include spring and hibernate in an application running on a Weblogic 10.3 server. When I run the application in the server, while accessing an TestServlet to check my configuration I get the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in class path resource [spring-config/HorizonModelPeopleConnectionsSpringContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:448)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
    at view.com.horizon.test.SpringHibernateServlet.doGet(SpringHibernateServlet.java:27)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at oracle.security.wls.filter.SSOSessionSynchronizationFilter.doFilter(SSOSessionSynchronizationFilter.java:279)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at oracle.dms.wls.DMSServletFilter.doFilter(DMSServletFilter.java:326)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3592)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:756)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:721)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:384)
    ... 31 more
Caused by: java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.class$(LocalSessionFactoryBean.java:158)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.(LocalSessionFactoryBean.java:158)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:85)
    ... 35 more

I have checked my application and the hibernate jar file is included and it contains the class it says its missing: org.hibernate.cfg.Configuration.

The application is built with maven. These are the dependencies of the JAR file using spring and hibernate:

    <!-- Frameworks -->
    <!-- Hibernate framework -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate</artifactId>
  <version>3.2.7.ga</version>
</dependency>
<!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.2</version>
</dependency>
<!-- Hibernate gives you a choice of bytecode providers between cglib and javassist -->
<dependency>
  <groupId>javassist</groupId>
  <artifactId>javassist</artifactId>
      <version>3.4.GA</version>
</dependency>
    <!-- Spring framework -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>2.5.6</version>
</dependency>

At first I thought it could be an issue with the versions in the spring and hibernate libraries, so I have tried with different ones, but still I couldn't find anywhere where it says which library versions are compatible,. just got that Spring 2.5.x needs hibernate >=3.1

And this is my Spring config file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="myDataSource"
          class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>jdbc/WebCenterDS</value>
        </property>
        <!--property name="resourceRef">
            <value>true</value>
        </property>
        <property name="jndiEnvironment">
            <props>
                <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
                <prop key="java.naming.provider.url">t3://localhost:7001</prop>
            </props>
        </property-->
    </bean>
    <bean id="mySessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="configLocation">
            <value>classpath:hibernate-config/hibernate.cfg.xml</value>
        </property>
        <property name="mappingResources">
            <list>
                <value>classpath:com/horizon/model/peopleconnections/profile/internal/bean/CustomAttribute.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
        </property>
    </bean>
    <bean id="profileExtensionDAO"
          class="com.horizon.model.peopleconnections.profile.internal.dao.ProfileExtensionDAOImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
</beans>

The WAR structure I get is the following:

J2EETestApplication
│   springhibernate.jsp
│
└───WEB-INF
    │   faces-config.xml
    │   web.xml
    │   weblogic.xml
    │
    ├───classes
    │   └───view
    │       └───com
    │           └───horizon
    │               └───test
    │                       SpringHibernateServlet.class
    │
    └───lib
            activation-1.1.jar
            antlr-2.7.6.jar
            aopallia开发者_运维技巧nce-1.0.jar
            asm-1.5.3.jar
            asm-attrs-1.5.3.jar
            cglib-2.1_3.jar
            commons-codec-1.3.jar
            commons-collections-2.1.1.jar
            commons-logging-1.1.1.jar
            dom4j-1.6.1.jar
            ehcache-1.2.3.jar
            hibernate-3.2.7.ga.jar
            horizon-model-commons-1.0-SNAPSHOT.jar
            horizon-model-peopleconnections-1.0-SNAPSHOT.jar
            horizon-shared-commons-1.0-SNAPSHOT.jar
            horizon-shared-logging-1.0-SNAPSHOT.jar
            horizon-shared-util-1.0-SNAPSHOT.jar
            horizon-shared-webcenter-1.0-SNAPSHOT.jar
            horizon-shared-webcenter.jar
            httpclient-4.0.1.jar
            httpcore-4.0.1.jar
            javassist-3.4.GA.jar
            jta-1.0.1B.jar
            log4j-1.2.14.jar
            mail-1.4.1.jar
            peopleconnections-profile-model-11.1.1.2.0.jar
            saxon-9.1.0.8.jar
            serviceframework-11.1.1.2.0.jar
            slf4j-api-1.5.2.jar
            slf4j-log4j12-1.5.2.jar
            spring-beans-2.5.6.jar
            spring-context-2.5.6.jar
            spring-core-2.5.6.jar
            spring-orm-2.5.6.jar
            spring-tx-2.5.6.jar

Is there any dependency or configuration I am missing?

If I use hibernate without spring using (with the same libraries) I don't get the ClassDefNotFoundException:

import java.net.URL;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            URL hibernateConfigURL = HibernateUtil.class.getClassLoader().getResource("hibernate-config/hibernate.cfg.xml");
            return new Configuration().configure(hibernateConfigURL).buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

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

That makes me think that is not a problem with the hibernate library not being properly picked. The Spring libraries seem to be picked properly as it starts to build the beans from the ApplicationContent. Could it be an issue of these two libraries not seeing each other?


My guess is that your application is not using the Spring JARs inside the webapp but the one provided by WebLogic and those can't "see" the Hibernate JAR inside the war.

To tell Weblogic to look into the war file, provide a WEB-INF/weblogic.xml with prefer-web-inf-classes set to true (this is required for Hibernate anyway because of the ANTLR version bundled in WLS):

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">
  <context-root>/myApp</context-root>
  <container-descriptor>
    <prefer-web-inf-classes>true</prefer-web-inf-classes>
  </container-descriptor>
</weblogic-web-app>


You have missed the hibernate-core dependency...example in the docs.

http://docs.jboss.org/hibernate/stable/core/reference/en/html/tutorial.html#tutorial-firstapp-mvn

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>


i had compile 'org.hibernate:hibernate-core:4.1.9'

i replaced it with compile 'org.hibernate:hibernate-core:3.3.2.GA'

this is gradle syntax, converting to maven isnt difficult


In addition to your answer @Rafeeq, this did the trick should incase you are using Maven dependency on pom.xml

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA</version>
        </dependency>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号