views:

1069

answers:

2

Hi,

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"&gt;
    <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
            aopalliance-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?

A: 

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>
khmarbaise
Hi, my first approach was to use org.hibernate:hibernate-core:3.3.2-GA instead of hibernate-3.2.7.ga.jar. Both already include the missing class, shouldn't it be anough with any of either?
Marcos Carceles
+3  A: 

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"&gt;
  <context-root>/myApp</context-root>
  <container-descriptor>
    <prefer-web-inf-classes>true</prefer-web-inf-classes>
  </container-descriptor>
</weblogic-web-app>
Pascal Thivent
again +1, and again my point is why isn't this the default setting :) You see - too many classpath problems arise from this, and you almost always have to use this (or similar) settings. Application servers are released more rarely compared to to small upgrades in bundled libraries.
Bozho
@Bozho I get your point but still, a parent first classloader strategy is what I would expect (using another strategy = know what you're doing).
Pascal Thivent
yes, you would expect it. And will know which setting to change in 99% of the cases. Others will not expect it :)That said - in what case actually it makes sense to have the bundled library loaded, instead of the one in the application?
Bozho
@Bozho servlet.jar, hibernate.jar in JBoss, etc :) It's actually a way to protect people from themselves. My point is that if you don't want to use the provided JAX-WS implementation, or the Spring supported version, etc then I think it makes sense to have to explicitly configure your application for this. It's a way to say I know what I'm doing, I'm responsible of my mistakes.
Pascal Thivent
here's the difference I think - servlet.jar and hibernate.jar are of different nature. servlet.jar is something that is "provided", because the server implements a certain version of the servlet api. And even if you put servlet-3.0.jar, it won't work. But hibernate is another thing - you don't (or shouldn't) rely on the container for that.
Bozho
Hi Pascal, thanks for the help. I could not test this as it introduces problems with some other (Oracle ADF / Faces) application components. If I come with a solution to this I'll let you know. Thank again.
Marcos Carceles