|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2007-04-11
刚刚看了下,确实是拿的session cache中的对象, 根本没有发送select 语句
|
|
| 返回顶楼 | |
|
时间:2007-04-11
dengyin2000 写道 刚刚看了下,确实是拿的session cache中的对象, 根本没有发送select 语句
这段代码的本意就是判断session中的对象是否被删除了。 前头漏了段
public void testDelete() {
Sample sample = dao.get(new Long(1));
dao.delete(sample);
Object obj = dao.get(new Long(1));
AssertNull(obj);
}
我不认为测试一定要直接操作数据库,尤其是用hibernate的时候。 |
|
| 返回顶楼 | |
|
时间:2007-04-14
dada 写道 我不认为测试一定要直接操作数据库,尤其是用hibernate的时候。
如果用了hibernate 测试的时候用hsqldb就可以了 |
|
| 返回顶楼 | |
|
时间:2007-05-10
我用 Hibernate delete 后 用Spring JdbcTemplate 查被删的记录还是存在,可用 Hibernate findByid 就会报 该记录已删 的错误.郁闷
|
|
| 返回顶楼 | |
|
时间:2007-05-10
这里是需要调用session的flunsh的,因为getJdbcTemplate().queryForInt方法不会flunsh缓存。
|
|
| 返回顶楼 | |
|
时间:2007-05-10
晨星★~雨泪 写道 我用 Hibernate delete 后 用Spring JdbcTemplate 查被删的记录还是存在,可用 Hibernate findByid 就会报 该记录已删 的错误.郁闷
hibernate delete只是在缓存中将某个持久对象给标记成删除的,但没有flunsh就实际没有执行删除操作,而JdbcTemplate不在session控制之下,因此还能从数据库找到标记为删除的记录。当然,被标记为删除的记录,再次通过hibernate来查找就会抛出异常,说已经被删除了。 |
|
| 返回顶楼 | |
|
时间:2007-05-11
我总觉得既然已删除 那么查出的结果应为NULL 才对,为什么要报错?
如果我 用 Hibernate findByid 去查一个数据表里没有的记录 那不是老抛异常. |
|
| 返回顶楼 | |
|
时间:2007-05-11
用session.get试试
|
|
| 返回顶楼 | |
|
时间:2007-05-15
我也遇到Spring AbstractTransactionalDataSourceSpringContextTests测试不可以回滚的问题。数据库是MYSQL。在测试里面新增记录的时候,竟然加数据库里面去了。而且更新和删除都可以回滚的!
public abstract class SpringTestCaseBase extends AbstractTransactionalDataSourceSpringContextTests { protected SimpleDateFormat sdf; public SpringTestCaseBase() { // query the protected variables to implement denpendency injection automatically, // so we don't need to write settor and gettor methods anymore. this.setPopulateProtectedVariables(true); sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setTimeZone(TimeZone.getDefault()); } protected String[] getConfigLocations() { return new String[] { "file:web/WEB-INF/applicationContext*.xml", "file:web/WEB-INF/test-ApplicationContext*.xml"}; } protected void flushSession(){ SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory"); sessionFactory.getCurrentSession().flush(); } } 这是我测试基类。 |
|
| 返回顶楼 | |
|
时间:2007-05-15
zhour560 写道 我也遇到Spring AbstractTransactionalDataSourceSpringContextTests测试不可以回滚的问题。数据库是MYSQL。在测试里面新增记录的时候,竟然加数据库里面去了。而且更新和删除都可以回滚的!
public abstract class SpringTestCaseBase extends AbstractTransactionalDataSourceSpringContextTests { protected SimpleDateFormat sdf; public SpringTestCaseBase() { // query the protected variables to implement denpendency injection automatically, // so we don't need to write settor and gettor methods anymore. this.setPopulateProtectedVariables(true); sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setTimeZone(TimeZone.getDefault()); } protected String[] getConfigLocations() { return new String[] { "file:web/WEB-INF/applicationContext*.xml", "file:web/WEB-INF/test-ApplicationContext*.xml"}; } protected void flushSession(){ SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory"); sessionFactory.getCurrentSession().flush(); } } 这是我测试基类。 看看你mysql数据库是什么类型的。 ISM类型的应该不能回滚,因为不支持事务。 换成innodb |
|
| 返回顶楼 | |










