Announcement Announcement Module
No announcement yet.
Problem using hibernate and unit testing Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem using hibernate and unit testing

    Reading this forum and documentations i use AbstractTransactionalSpringContextTests to implement dao unit testing
    hoping that the test didn't modify the database, this work perfectly in many cases, but for example when i do the following i get problems: I load an entity from DB, then i modify a property and next i go to do a read operation from DB to load a list of objects (including the loaded before) this provoke that hibernate automatically execute an
    update over a DB before the reading operation to reflect the changes we made before (the modified property) , when the test finished this modification remain in the DB, In hibernate documentation this can be disable putting in the property FlushMode of the Hibernate Session COMMIT (by default is set to AUTO)

    I want to know is how to modify this property from Spring' xml or by code, or if exists other solution to the problem,
    here attached the config xml for the test.

    Best Regards, Rodney


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value> ver</value></property>
    <property name="url"><value>jdbc:mysql://localhost:3306/rdk?autoReconnect=true</value></property>
    <property name="username"><value>root</value></property>
    <property name="password"><value></value></property>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <property name="hibernateProperties">
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.M ySQLDialect</prop>
    <prop key="hibernate.query.substitutions">true=1 false=0</prop>
    <prop key="hibernate.show_sql">true</prop>
    <property name="dataSource"><ref local="dataSource"/></property>

    <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>

    <bean id="userDao" class=" DaoImpl">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>


  • #2
    I'm a bit puzzled. This scenario should work. It doesn't matter if Hibernate flushes its changes by issuing JDBC updates--it should all be in the context of a tx that will get rolled back. Indeed I have done the same thing in tests successfully many times. You are doing all these operations in one testXXXX method in a subclass of AbstractTransactionalSpringContextTests? Can you please post one of your test methods that's showing this problem?

    Note that you should not use any transaction methods in the Hibernate API, but leave the tx mgt to Spring.


    • #3
      From :

      Matt Raible wrote : You have to use InnoDB tables for MySQL in order to get transactional capabilities:

      On Windows, the easiest way to do this is to stop the Mysql
      service, add "--default-table-type=InnoDB" to the startup
      parameters (no quotes) and then start the service.

      Is this the issue in your case?



      • #4
        Example of test

        Here i put a test code that make that issue, but now i going to check about the table type "InnoDB" in mySQL to have transactional capabilities, maybe this is the problem, later i post again the results.

        Thanks for your help, Spring is a great job!!!


        • #5

          public void testCanSaveUpdateObjectWithSameLogin() {
          userDao = (UserDao) applicationContext.getBean("userDao");
          User usr = new User();
          FillUserObject(usr, null);
          assertEquals("This method must return false", false, userDao.canSave(usr));


          • #6
            Finally it works

            The problem was the type of the table i configure all to use InnoDB and the test works perfectly.

            Thanks sjivan