jpa 2 hibernate在EntityManager.find上设置锁

jpa 2 hibernate在EntityManager.find上设置锁,hibernate,orm,jpa,locking,jpa-2.0,Hibernate,Orm,Jpa,Locking,Jpa 2.0,我正在一个hibernate项目上做一些测试用例: 当我打电话时 EntityManager em = getEntityManager(); em.find(Foo.class, 1) 我得到了预期的实体,但调用时: EntityManager em = getEntityManager(); em.find(Foo.class, 1, LockModeType.WRITE) 我越来越空了。此外,当我作出以下决定时: EntityManager em = getEntityManager(

我正在一个hibernate项目上做一些测试用例:

当我打电话时

EntityManager em = getEntityManager();
em.find(Foo.class, 1)
我得到了预期的实体,但调用时:

EntityManager em = getEntityManager();
em.find(Foo.class, 1, LockModeType.WRITE)
我越来越空了。此外,当我作出以下决定时:

EntityManager em = getEntityManager();
Foo foo = em.find(Foo.class, 1)
em.lock(foo, LockModeType.WRITE);
我得到了目标,它的工作正如我所期望的

编辑:

我的依赖项:

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.5.5-Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.5.0-Beta-2</version>
    </dependency>
    <dependency>
        <groupId>jboss</groupId>
        <artifactId>jboss</artifactId>
        <version>4.2.3.GA</version>
        <scope>provided</scope>
    </dependency>

org.hibernate
冬眠核心
3.5.5-最终版本
org.hibernate
休眠验证器
4.1.0.1最终版本
org.hibernate
休眠实体管理器
3.5.0-Beta-2
jboss
jboss
4.2.3.GA
假如

你能给我一个分数吗?

我无法复制。与以下实体的合作:

@Entity
public class Foo {
    @Id private Integer id;
    private String name;
    @Version private Integer version;
    ...
}
以下代码段正好有效:

EntityManager em = getEntityManager();
Foo foo = em.find(Foo.class, 1, LockModeType.WRITE); 
assertNotNull(foo);
foo.setName("baz");
em.flush();
并使用乐观锁定读取和更新实体,版本更新:

10:21:42.223 [main] DEBUG org.hibernate.SQL - select foo0_.id as id25_0_, foo0_.name as name25_0_, foo0_.version as version25_0_ from Foo foo0_ where foo0_.id=? 10:21:42.225 [main] TRACE org.hibernate.type.LongType - binding '1' to parameter: 1 10:21:42.229 [main] TRACE org.hibernate.type.StringType - returning 'bar' as column: name25_0_ 10:21:42.230 [main] TRACE org.hibernate.type.LongType - returning '0' as column: version25_0_ 10:21:42.246 [main] DEBUG org.hibernate.SQL - update Foo set name=?, version=? where id=? and version=? 10:21:42.248 [main] TRACE org.hibernate.type.StringType - binding 'baz' to parameter: 1 10:21:42.249 [main] TRACE org.hibernate.type.LongType - binding '1' to parameter: 2 10:21:42.249 [main] TRACE org.hibernate.type.LongType - binding '1' to parameter: 3 10:21:42.250 [main] TRACE org.hibernate.type.LongType - binding '0' to parameter: 4
我尝试使用相同的版本,但仍然存在问题。太好了,现在似乎可以了。。但我发现了另一件事(要编辑帖子,这里有点小)@Hugo好的,很高兴问题解决了。关于新问题,这是另一个问题。因此,请打开一个新的测试(最好的做法是不要混合主题),并解释为什么这个测试应该抛出异常,我不明白为什么它应该抛出异常。 10:21:42.223 [main] DEBUG org.hibernate.SQL - select foo0_.id as id25_0_, foo0_.name as name25_0_, foo0_.version as version25_0_ from Foo foo0_ where foo0_.id=? 10:21:42.225 [main] TRACE org.hibernate.type.LongType - binding '1' to parameter: 1 10:21:42.229 [main] TRACE org.hibernate.type.StringType - returning 'bar' as column: name25_0_ 10:21:42.230 [main] TRACE org.hibernate.type.LongType - returning '0' as column: version25_0_ 10:21:42.246 [main] DEBUG org.hibernate.SQL - update Foo set name=?, version=? where id=? and version=? 10:21:42.248 [main] TRACE org.hibernate.type.StringType - binding 'baz' to parameter: 1 10:21:42.249 [main] TRACE org.hibernate.type.LongType - binding '1' to parameter: 2 10:21:42.249 [main] TRACE org.hibernate.type.LongType - binding '1' to parameter: 3 10:21:42.250 [main] TRACE org.hibernate.type.LongType - binding '0' to parameter: 4
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.5.5-Final</version>
</dependency>
<dependency>
    <groupId>jboss</groupId>
    <artifactId>jboss</artifactId>
    <version>4.2.3.GA</version>
    <scope>provided</scope>
</dependency>