0

我对 HSQL 数据库的 Hibernate 连接进行了以下测试,使用
import static org.springframework.test.jdbc.SimpleJdbcTestUtils.countRowsInTable;
来计算表中的行数,但删除一行后结果仍然相同。我的代码有什么问题?

更新:jdbcTemplate和 HibernateSessionFactory使用相同的 dataSource bean。

package com.habuma.spitter.persistence;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.jdbc.SimpleJdbcTestUtils.countRowsInTable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

import com.habuma.spitter.domain.Spitter;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:persistence-context.xml",
                "classpath:test-dataSource-context.xml",
                "classpath:test-transaction-context.xml"})
@TransactionConfiguration(transactionManager="txMgr", defaultRollback=true)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
@Transactional
public class DataAccessUnitTestTemplate {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private SpitterDao spitterDao;

    @Autowired
    private SimpleJdbcTemplate jdbcTemplate;

    @Test
    public void testDataAccess() {

        Spitter spitter = new Spitter();
        spitter.setFullName("First");
        spitter.setEmail("one@gmail.com");
        spitter.setUsername("onefirst");
        spitter.setPassword("password");
        spitter.setUpdateByEmail(false);

        //add to database
        spitterDao.addSpitter(spitter);

        //update user with id = 3
        Spitter toChangeSpitter = spitterDao.getSpitterById(3);
        toChangeSpitter.setFullName("NULL");
        spitterDao.saveSpitter(toChangeSpitter);

        //add next user
        spitter = new Spitter();
        spitter.setFullName("Second");
        spitter.setEmail("two@gmail.com");
        spitter.setUsername("twosecond");
        spitter.setPassword("pass");
        spitter.setUpdateByEmail(false);
        spitterDao.addSpitter(spitter);

        assertEquals(4, countRowsInTable(jdbcTemplate, "spitter"));

        for(int i = 1; i <= 4; i++) {
            spitter = spitterDao.getSpitterById((long)i);
            System.out.println(spitter.getId() + " : " + spitter.getUsername() + " : " + spitter.getFullName());
        }

        //delete user with ID = 4
        spitterDao.deleteSpitter(4);

        System.out.println("\n");
        for(int i = 1; i <= 3; i++) {
            spitter = spitterDao.getSpitterById((long)i);
            System.out.println(spitter.getId() + " : " + spitter.getUsername() + " : " + spitter.getFullName());
        }
        System.out.println("Ehm ... " + spitterDao.getSpitterById(4));  //null
        assertEquals(3, countRowsInTable(jdbcTemplate, "spitter")); //assertion ? rows still 4
    }

}

这是输出

1 : habuma : Craig Walls
2 : artnames : Art Names
3 : onefirst : NULL
4 : twosecond : Second


1 : habuma : Craig Walls
2 : artnames : Art Names
3 : onefirst : NULL
Ehm ... null
>>!Assertion!<<
java.lang.AssertionError: expected:<3> but was:<4>
4

1 回答 1

0

为了确保你得到你期望得到的东西,你需要在每个断言之前刷新 Hibernate 会话。否则有些东西可能会被缓存在会话中,而不是在数据库上执行。这种方式调用 Hibrnate 函数将返回正确的结果,因为它会知道缓存,但是直接通过 JDBC 绕过它,将看不到缓存并返回 strage 内容。

于 2013-09-04T20:33:42.113 回答