开发者

jpa 2 hibernate setting a lock on EntityManager.find

I'm doing a few testcases on an hibernate project:

when I call

EntityManager em = getEntityManager();
em.find(Foo.class, 1)

I get the entity as I expect, but when I invoke:

EntityManager em = getEntityManager();
em.find(Foo.class, 1, LockModeType.WRITE)

I'm getting null. Also, when I make:

EntityManager em = getEntityMan开发者_如何转开发ager();
Foo foo = em.find(Foo.class, 1)
em.lock(foo, LockModeType.WRITE);

I'm getting the object, and it's working as I expect.

EDIT:

@javax.persistence.Entity
@Table(name="foo")
static class Foo implements Serializable {
    @Id private Integer id;
    private String code;
    @Version private Integer version;

    public Foo() {
    }
            ........        
}

My dependencies:

<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>

Can you give me a point?


I cannot reproduce. With the following entity:

@Entity
public class Foo {
    @Id private Integer id;
    private String name;
    @Version private Integer version;
    ...
}

The following snippet just works:

EntityManager em = getEntityManager();
Foo foo = em.find(Foo.class, 1, LockModeType.WRITE); 
assertNotNull(foo);
foo.setName("baz");
em.flush();

And reads and updates the entity using optimistic locking, with version update:

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

Tested with Hibernate 3.5.5-Final.


You are not using the same version (I mentioned it explicitly because of issues like HHH-5032). Try with the following dependencies instead (you don't need to specify a dependency on the hibernate-core artifact, you'll get it transitively):

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜