Announcement Announcement Module
Collapse
No announcement yet.
acces data with spring hibernate integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • acces data with spring hibernate integration

    hello,
    i have an issue to access the data in mySQL database, with Hibernate framework.

    To access to it, I'm using a Spring configuration (xml file), but I got a null pointer exception, so I cannot display the content of a table in my Eclipse console.

    Here 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" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schem...ng-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/base" />
    <property name="username" value="root" />
    <property name="password" value="admin" />
    </bean>

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
    <value>hibernate.test.web.model.Acteur</value>
    <value>hibernate.test.web.model.Affectation</value>
    <value>hibernate.test.web.model.Catalogue</value>
    <value>hibernate.test.web.model.Conges</value>
    <value>hibernate.test.web.model.Consotaches</value>
    <value>hibernate.test.web.model.Devis</value>
    <value>hibernate.test.web.model.Enveloppes</value>
    <value>hibernate.test.web.model.EnveloppesId</value>
    <value>hibernate.test.web.model.Profil</value>
    <value>hibernate.test.web.model.Projet</value>
    <value>hibernate.test.web.model.Risques</value>
    <value>hibernate.test.web.model.Tachesdevis</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQ LInnoDBDialect</prop>
    </props>
    </property>
    </bean>

    <bean id ="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config />
    <context:component-scan base-package="hibernate.test.web" />

    </beans>

    Thanks for your help.

  • #2
    Hi, you can see an example to integrate spring and hibernate here (http://www.apress.com/downloadable/d...sample_id/556/).

    ...\dataaccess\src\main\java\com\apress\springente rpriserecipes\course\hibernate

    Comment


    • #3
      thank you for answer.
      but now i have an other exception when i am trying to execute my test class,
      the exception is :

      org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:881)
      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:837)
      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:220)
      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:729)
      at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:381)
      at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
      at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
      at hibernate.test.web.service.ActeurServiceTest.setUp BeforeClass(ActeurServiceTest.java:29)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:44)
      at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
      at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:41)
      at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:27)
      at org.junit.internal.runners.statements.RunAfters.ev aluate(RunAfters.java:31)
      at org.junit.runners.ParentRunner.run(ParentRunner.ja va:220)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:46)
      at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)
      Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
      at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:111)
      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:875)
      ... 32 more
      Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
      at org.hibernate.cfg.Configuration.<clinit>(Configura tion.java:196)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:169)
      at org.springframework.orm.hibernate3.LocalSessionFac toryBean.class$(LocalSessionFactoryBean.java:174)
      at org.springframework.orm.hibernate3.LocalSessionFac toryBean.<init>(LocalSessionFactoryBean.java:174)
      at org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean.<init>(AnnotationSessionF actoryBean.java:64)
      at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
      at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:96)
      ... 34 more
      Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
      at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
      ... 45 more

      java.lang.NullPointerException
      at hibernate.test.web.service.ActeurServiceTest.tearD ownAfterClass(ActeurServiceTest.java:35)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:44)
      at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
      at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:41)
      at org.junit.internal.runners.statements.RunAfters.ev aluate(RunAfters.java:37)
      at org.junit.runners.ParentRunner.run(ParentRunner.ja va:220)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:46)
      at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)


      thanks for your help.

      Comment


      • #4
        Hi, you get "NoClassDefFoundError: org/slf4j/LoggerFactory" try to add this dependency:

        Code:
        		<dependency>
        			<groupId>org.slf4j</groupId>
        			<artifactId>slf4j-log4j12</artifactId>
        			<version>1.6.1</version>
        			<scope>runtime</scope>
        		</dependency>

        Comment


        • #5
          thanks for the answer

          Comment


          • #6
            hello,
            l have an other issure when i ma trying to deploy my application with Jboss server , my exception looks like that:

            at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.wrapIfNecessary(AbstractAutoPro xyCreator.java:345) [:2.5.4]
            at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.postProcessAfterInitialization( AbstractAutoProxyCreator.java:309) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyBeanPostProcessors AfterInitialization(AbstractAutowireCapableBeanFac tory.java:361) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1342) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:471) [:2.5.4]
            ... 92 more
            Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'transactionManager' defined in class path resource [application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactor y()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
            at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:275) [:2.5.4]
            at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:104) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1244) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1008) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:470) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409) [:2.5.4]
            at java.security.AccessController.doPrivileged(Native Method) [:1.6.0_31]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380) [:2.5.4]
            at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveInnerBean(BeanDefinitio nValueResolver.java:219) [:2.5.4]
            ... 113 more
            Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'transactionManager' defined in class path resource [application-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactor y()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
            at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:275) [:2.5.4]
            at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:104) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1244) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1008) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:470) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409) [:2.5.4]
            at java.security.AccessController.doPrivileged(Native Method) [:1.6.0_31]
            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64) [:2.5.4]
            at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:220) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 ) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185) [:2.5.4]
            at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164) [:2.5.4]
            at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:269) [:2.5.4]
            .
            thanks to give me a solution.

            Comment


            • #7
              OK, you get "org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature" try to find the slf4j that JBoss's already using and use it instead of what I've said previously.

              Comment

              Working...
              X