views:

112

answers:

1
Hi guys: a little strange problem.. 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: org.hibernate hibernate-core 3.5.5-Final org.hibernate hibernate-validator 4.1.0.Final org.hibernate hibernate-entitymanager 3.5.0-Beta-2 jboss jboss 4.2.3.GA provided Can you give me a point? Thanks.
+1  A: 

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>
Pascal Thivent
I try with the same version, but still having the issue.
Hugo
Great, it seems to work now.. but I found another thing (going to edit the post, little small here)
Hugo
@Hugo Ok, glad it's solved. Regarding the new problem, this is another question. So please open a new one (the best practice is to not mix topics) and explain why this test is supposed to throw an exception, I don't see why it should.
Pascal Thivent