Results 1 to 6 of 6

Thread: Problem using hibernate and unit testing

  1. #1
    Join Date
    Oct 2004
    Location
    Havana, Cuba
    Posts
    25

    Default 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. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Sep 2004
    Location
    Boston, US
    Posts
    130

    Default

    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

  4. #4
    Join Date
    Oct 2004
    Location
    Havana, Cuba
    Posts
    25

    Default 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

  5. #5
    Join Date
    Oct 2004
    Location
    Havana, Cuba
    Posts
    25

    Default 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));
    }

  6. #6
    Join Date
    Oct 2004
    Location
    Havana, Cuba
    Posts
    25

    Default 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

Similar Threads

  1. Unit Testing
    By phayte in forum Data
    Replies: 1
    Last Post: Aug 3rd, 2005, 08:42 PM
  2. Replies: 1
    Last Post: Jul 13th, 2005, 04:32 PM
  3. Replies: 2
    Last Post: Jul 8th, 2005, 10:37 PM
  4. Replies: 12
    Last Post: May 18th, 2005, 04:57 PM
  5. Replies: 2
    Last Post: Sep 5th, 2004, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •