Announcement Announcement Module
Collapse
No announcement yet.
Spring hibernate configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring hibernate configuration

    Hi everybody,
    I'm trying to use spring and hibernate. here is my beans.xml.
    PHP Code:
    <!-- DataSource Property -->
        <
    bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
         <
    property name="driverClassName">
           <
    value>com.mysql.jdbc.Driver</value>
         </
    property>
         <
    property name="url">
           <
    value>jdbc:mysql://localhost/db</value>
         
    </property>
         <
    property name="username">
             <
    value>user</value>
         </
    property>
         <
    property name="password">
             <
    value>pass</value>
         </
    property>
        </
    bean
        
        <!-- 
    Hibernate SessionFactory -->
        <
    bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
         <
    property name="dataSource">
             <
    ref local="dataSource"/>
         </
    property>
         
         <
    property name="hibernateProperties">
             <
    props>
               <
    prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
               <
    prop key="hibernate.query.substitutions">true 'T'false 'F'</prop>
               <
    prop key="hibernate.show_sql">true</prop>
               <
    prop key="hibernate.c3p0.minPoolSize">5</prop>
               <
    prop key="hibernate.c3p0.maxPoolSize">20</prop>
               <
    prop key="hibernate.c3p0.timeout">600</prop>
               <
    prop key="hibernate.c3p0.max_statement">50</prop>
               <
    prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
              </
    props>
         </
    property>
         
         <!--  
    Object-Relational mapping files. -->
         <
    property name="mappingResources">
            <list>
               <
    value>ch/orange/rps_ws/Country.hbm.xml</value>
            </list>
         </
    property>
        </
    bean
    I think it's correct, but when I try to start the server I get the following error:
    Context initialization failed
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFac toryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:883)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:839)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:440)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:423)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:380)
    at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 45)
    at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:3843)
    at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4342)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(Sta ndardService.java:516)
    at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalin a.java:578)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:413)
    Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFac toryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:115)
    at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:61)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:877)
    ... 31 more
    Caused by: java.lang.NoClassDefFoundError
    at org.springframework.orm.hibernate3.LocalSessionFac toryBean.class$(LocalSessionFactoryBean.java:174)
    at org.springframework.orm.hibernate3.LocalSessionFac toryBean.<init>(LocalSessionFactoryBean.java:174)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:100)
    ... 33 more
    Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
    at org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1233)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    ... 40 more

    any ideas why this is happening?
    I checked my buildPath and both spring and hibernate jars are in it.

    this is driving me crazy!!!

  • #2
    maybe the pom.xml can help figure this out:

    PHP Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>ch.orange.rps_ws</groupId>
        <artifactId>rps_ws</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>RPS WS</name>
        <dependencies>
             <!--  load cglib without dependencies -->
             <dependency>
                  <groupId>cglib</groupId>
                   <artifactId>cglib-nodep</artifactId>
                   <version>2.1_3</version>
            </dependency>
            <!-- <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
                <version>2.1.4</version>
                <exclusions> -->
                
                     <!-- exclude spring because it's 2.0 and will use 2.5.6 (later)
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion> -->
                    
                     <!-- exclude cglib because creates conflicts with cxf 
                    <exclusion>
                        <groupId>cglib</groupId>
                        <artifactId>cglib</artifactId>
                    </exclusion>
                    
                </exclusions>
            </dependency>-->
            <!-- <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-transports-http</artifactId>
                <version>2.1.4</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                    </exclusion> -->
                    
                    <!-- exclude cglib because creates conflicts with cxf 
                    <exclusion>
                        <groupId>cglib</groupId>
                        <artifactId>cglib</artifactId>
                    </exclusion>
                    
                </exclusions>
            </dependency>-->
            <!-- jaxws-api 2.0 because of incompatibility with web service --> 
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
                <version>2.5.6</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.4</version>
            </dependency>
            <!--  hibernate -->
            <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate</artifactId>
              <version>3.2.4.ga</version>
              <scope>test</scope>
              <exclusions>
              <!-- exclude cglib because creates conflicts with cxf -->
                    <exclusion>
                        <groupId>cglib</groupId>
                        <artifactId>cglib</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- include commons dpcp for datasource -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.2.2</version>
            </dependency> 

            <!--  database / mysql temp -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        
        <build>
            <pluginManagement>
                <plugins>
                    
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <source>1.5</source>
                            <target>1.5</target>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-eclipse-plugin</artifactId>
                        <configuration>
                            <projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
                            <wtpmanifest>true</wtpmanifest>
                            <wtpapplicationxml>true</wtpapplicationxml>
                            <wtpversion>2.0</wtpversion>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.0.2</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    the commented rows concern CXF and I commented them to figure the hibernate error first!

    Comment


    • #3
      this log is saying you don't have necessary jars , dou you have spring-orm.jar ??

      Comment


      • #4
        Originally posted by duardito View Post
        this log is saying you don't have necessary jars , dou you have spring-orm.jar ??
        I include the whole spring in my pom.. I should have everything I need.. or not? (I'm new to this stuff).

        attached is what I see in my buildpath:

        Comment


        • #5
          I think that the problem is a missing library.

          In the case that all jar have been added, another reason may be the versions of hibernate and spring.

          What version of Hibernate and Spring are you using?

          greetings!

          Comment


          • #6
            Originally posted by s.contreras View Post
            I think that the problem is a missing library.

            In the case that all jar have been added, another reason may be the versions of hibernate and spring.

            What version of Hibernate and Spring are you using?

            greetings!
            spring 2.5.6
            hibernate 3.2.4

            Comment


            • #7
              with those versions the application will must working fine.

              Check the dependencies and scope dependecies of the POM, it seems that you lack any dependency.

              Watch the dependencies that are called from the class org.hibernate.cfg.Configuration and you do not have added to the project.
              Last edited by s.contreras; Aug 24th, 2009, 06:13 AM.

              Comment


              • #8
                Originally posted by s.contreras View Post
                with those versions the application will must working fine.

                Check the dependencies and scope dependecies of the POM, it seems that you lack any dependency.

                Watch the dependencies that are called from the class org.hibernate.cfg.Configuration and you do not have added to the project.
                thanks for your help!!! The problem was related to the scope:
                hibernate scope was "Test"... now I removed all the scopes and it seems to work.
                you gave me valuable support.. thanks a lot.
                btw.. what exactly does the scope parameter do?

                Comment


                • #9
                  Your welcome homer84.

                  I think that the solution is not remove all the scopes...jeje.

                  The scope in Maven define the limit of transitivity of dependency. In other words, lets you use certain JARs only when you really need them and excludes them from the classpath when you don't.

                  This item is from another forum and insurance that can help you better than me

                  Watch this reference:
                  http://maven.apache.org/guides/intro...pendency_Scope

                  greetings!

                  Comment


                  • #10
                    Originally posted by s.contreras View Post
                    Your welcome homer84.
                    I think that the solution is not remove all the scopes...jeje.
                    Hehe.. well, of course! but removing them solved the problem, meaning that I had to set them properly... I'll work on that later!

                    Comment

                    Working...
                    X