单元测试Hibernate的乐观锁定(在Spring中) [英] Unit Testing Hibernate's Optimistic Locking (within Spring)

查看:94
本文介绍了单元测试Hibernate的乐观锁定(在Spring中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个单元测试来验证乐观锁定是否正确设置(使用Spring和Hibernate)。



我想要测试类扩展Spring的 AbstractTransactionalJUnit4SpringContextTests



我最终想要的是这样的方法:

  
@Test(expected = StaleObjectStateException.class)
public void testOptimisticLocking(){
A a = getCurrentSession()。load(A.class,1);
a.setVersion(a.getVersion() - 1);
getCurrentSession()。saveOrUpdate(a);
getCurrentSession()。flush();
失败(乐观锁定不起作用);
}

此测试失败。作为最佳做法,你推荐什么?



我尝试这样做的原因是我想要转换版本给客户端(使用DTO)。我想证明,当DTO被发送回服务器并与新加载的实体合并时,如果在此期间由其他人更新,则保存该实体将失败。

解决方案

看起来,它不是一个将DTO中的所有字段(包括版本)转移到新加载的实体,尝试保存它并获取异常如果实体在客户端修改DTO时被修改过。

原因是Hibernate根本不关心你对版本字段做了什么,因为你在同一场会议上工作。版本字段的值由会话记住。



一个简单的证明:

  
@Test(expected = StaleObjectStateException.class)
public void testOptimisticLocking(){$ b $ a A = getCurrentSession()。load(A.class,1);
getCurrentSession()。evict(a); //注释掉,测试失败
a.setVersion(a.getVersion() - 1);
getCurrentSession()。saveOrUpdate(a);
getCurrentSession()。flush();
失败(乐观锁定不起作用);
}

感谢大家的帮助!


I'd like to write a unit test to verify that optimistic locking is properly set up (using Spring and Hibernate).

I'd like to have the test class extend Spring's AbstractTransactionalJUnit4SpringContextTests.

What I want to end up with is a method like this:


@Test (expected = StaleObjectStateException.class) 
public void testOptimisticLocking() {
    A a = getCurrentSession().load(A.class, 1);
    a.setVersion(a.getVersion()-1);
    getCurrentSession().saveOrUpdate(a);
    getCurrentSession().flush();           
    fail("Optimistic locking does not work");
}

This test fails. What do you recommend as a best practice?

The reason I am trying to do this is that I want to transfer the version to the client (using a DTO). I want to prove that when the DTO is sent back to the server and merged with a freshly loaded entity, saving that entity will fail if it's been updated by somebody else in the meantime.

解决方案

It seems that it simply isn't an option to transfer all the fields from a DTO (including version) to a freshly loaded entity, try to save it, and get an exception in case the entity was modified while the DTO was being modified on the client.

The reason for that is that Hibernate simply doesn't care what you do to the version field, given that you're working in the same session. The value of the version field is remembered by the session.

A simple proof of that:


@Test (expected = StaleObjectStateException.class) 
public void testOptimisticLocking() {
    A a = getCurrentSession().load(A.class, 1);
    getCurrentSession().evict(a); //comment this out and the test fails
    a.setVersion(a.getVersion()-1);
    getCurrentSession().saveOrUpdate(a);
    getCurrentSession().flush();           
    fail("Optimistic locking does not work");
}

Thanks everyone for help anyway!

这篇关于单元测试Hibernate的乐观锁定(在Spring中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆