Announcement Announcement Module
Collapse
No announcement yet.
Problem using hibernate and unit testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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

    <beans>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>org.gjt.mm.mysql.Dri 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>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <list>
    <value>common.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <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>
    </props>
    </property>
    <property name="dataSource"><ref local="dataSource"/></property>
    </bean>

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

    <bean id="userDao" class="com.rdk.security.persistence.hibernate.User DaoImpl">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    </beans>

  • #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.

    Comment


    • #3
      From :
      https://appfuse.dev.java.net/servlet...23&forumID=566

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

      http://www.hibernate.org/119.html#A8

      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?

      Sanjiv

      Comment


      • #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!!!
        Rodney

        Comment


        • #5
          Code

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

          Comment


          • #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
            Rodney

            Comment

            Working...
            X