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

  • hibernate - spring

    Hi,
    i've tried to follow a tutoriel about hibernate integration in a spring project. Unnfortunatly with all my shame, I have a probleme sw that i dont know. Id be very happy to get yur help

    Here is my spring configuration file:
    <beans>
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/tp"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
    <list>
    <value>tp/spring/model/person.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <value>
    hibernate.format_sql=true
    hibernate.dialect=org.hibernate.dialect.MySQLDiale ct
    </value>
    </property>

    </bean>

    <bean id="personneDao" class="tp.spring.dao.PersonDaoImp">
    <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    And here is PersonDapImp class:
    public class PersonDaoImp
    {
    private HibernateTemplate hibernateTemplate;
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
    hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    public List<Person> getAllPersons() throws DataException {
    return this.sessionFactory.getCurrentSession().createQuer y("from Person") .list();
    }
    }
    and my test class :
    beanFactory = new XmlBeanFactory(new ClassPathResource("ApplicationContexte.xml"));
    List<Person> list = (List<Person>)beanFactory.getBean("personneDao");
    System.out.println("list size: "+list.size());
    and finaly here are the errors occured
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'personneDao' defined in class path resource [ApplicationContexte.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.CannotLoadBeanCl assException: Error loading class [org.springframework.orm.hibernate3.LocalSessionFac toryBean] for bean with name 'mySessionFactory' defined in class path resource [ApplicationContexte.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator
    Caused by: org.springframework.beans.factory.CannotLoadBeanCl assException: Error loading class [org.springframework.orm.hibernate3.LocalSessionFac toryBean] for bean with name 'mySessionFactory' defined in class path resource [ApplicationContexte.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator
    Caused by: java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator
    at java.lang.ClassLoader.defineClass1(Native Method)
    ...........
    thanks for help
    Last edited by khaigo; May 26th, 2009, 11:58 AM.

  • #2
    What jar files are you including in your project? That class is found in either "spring.jar" or "spring-tx.jar". What version of Spring are you trying to use?

    Comment


    • #3
      first of all, thanks for replying,
      I am using:
      • spring-hibernate3.jar
      • hibernate3.jar
      • spring-beans.jar
      • spring-core.jar
      • mysql-connector-java5.1.5.jar
      .......
      in my PersonDapImp, i m using:
      • org.hibernate.SessionFactory;
      • org.springframework.orm.hibernate3.HibernateTempla te

      Comment


      • #4
        Ok, so the problem is obvious. You're not using either of the jar files that contains that class. If you're going to go "ala carte" with the various Spring module jars, instead of the all-inclusive "spring.jar", then you should include "spring-tx.jar".

        Comment


        • #5
          Oh I am realy sorry but i didn t get your answer, I ve added spring.jar and nothing has been executed.

          All my classes are somewhere in a jar package
          org.springframework.orm.hibernate3.HibernateTempla te EXISTS in spring-hibernate3.jar
          and
          org.hibernate.SessionFactory ESISTS in hibernate3.jar
          and
          org.springframework.core.io.ClassPathResource EXISTS in spring-beans.jar
          thanks a lot for help

          Comment


          • #6
            I'm assuming from what you're saying that you're still having the problem?

            Please list again exactly what jar files you are using in your project.

            Comment


            • #7
              yes i still have the probleme. Here are the jars i m using:
              • spring-core.jar
              • spring-beans.jar
              • spring.jar
              • spring-hibernate3.jar
              • mysql-connector5.1.5-bean.jar
              And hibernate jars of course.
              I m using these classes:
              org.springframework.core.io.ClassPathResource FROM spring-core.jar
              and
              org.springframework.beans.factory.ListableBeanFact ory FROM spring-beans.jar
              and
              org.springframework.orm.hibernate3.HibernateTempla te FROM spring-hibernate3.jar
              here are the errors occured:
              Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'personneDao' defined in class path resource [ApplicationContexte.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'mySessionFactory' defined in class path resource [ApplicationContexte.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
              Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'mySessionFactory' defined in class path resource [ApplicationContexte.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
              Caused by: java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
              at java.lang.Class.getDeclaredMethods0(Native Method)
              at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
              at java.lang.Class.getDeclaredMethods(Unknown Source)
              ..
              .
              Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
              at java.net.URLClassLoader$1.run(Unknown Source)
              at java.security.AccessController.doPrivileged(Native Method)
              at java.net.URLClassLoader.findClass(Unknown Source)
              at java.lang.ClassLoader.loadClass(Unknown Source)
              at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

              Comment


              • #8
                The key information from those error messages is what class it isn't finding. The first time you reported this, it was looking for "org.springframework.dao.support.PersistenceExcept ionTranslator". Now it's looking for "javax.transaction.TransactionManager". You can find that class in "lib/j2ee/jta.jar" in the Spring distribution. If you get an error like this again, pay attention to what class it can't find.

                Comment


                • #9
                  Well thanks for help, i ve added the jar with some others requested,
                  Now i still have a small probleme with casting (I very new with this)

                  Exception in thread "main" java.lang.ClassCastException: tp.spring.dao.PersonDaoImp cannot be cast to java.util.List
                  at tp.spring.tests.Test.main(Test.java:30)
                  Here is my PersonDaoImp:
                  private HibernateTemplate hibernateTemplate;
                  private SessionFactory sessionFactory;
                  public void setSessionFactory(SessionFactory sessionFactory) {
                  this.hibernateTemplate = new HibernateTemplate(sessionFactory);
                  }

                  public List<Person> getAllPersons() throws DataException {
                  return this.hibernateTemplate.find("form Person");
                  }
                  and here is my test class:
                  beanFactory = new XmlBeanFactory(new ClassPathResource("ApplicationContexte.xml"));
                  List<Person> list = (List<Person>)beanFactory.getBean("personneDao");
                  System.out.println("list size: "+list.size());

                  Comment


                  • #10
                    Ok the probleme has been resolved, i didnt pay attention to the object i should create.

                    Thanks a lot for your help

                    Comment

                    Working...
                    X