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 = getEntityManager();
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:
The following snippet just works:
And reads and updates the entity using optimistic locking, with version update:
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-coreartifact, you’ll get it transitively):