Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1055511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:34:21+00:00 2026-05-16T17:34:21+00:00

I’m doing a few testcases on an hibernate project: when I call EntityManager em

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:34:22+00:00Added an answer on May 16, 2026 at 5:34 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.