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

  • SEARCH
  • Home
  • 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 7054755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:39:56+00:00 2026-05-28T03:39:56+00:00

When I use em.persist() to insert a new instance of an entity-class, generated id

  • 0

When I use em.persist() to insert a new instance of an entity-class, generated id is reflected in entity-class, but the version property still shows null, it is not reflected in the entity-class.

I’m using GlassFish 3.1.1.

partial entity-class code

@Id
@GeneratedValue
private Long id;

@Version
private Long version;

public void create(Item item) {

    System.out.println("Before Creating " + item.id + ", " + item.version);
    entityManager.persist(item);
    System.out.println("After Creating " + item.id + ", " + item.version);
}

Statement after persist() displays item.id shows 1, but item.version shows null, even though, if I check in database it is 1.

Is it supposed to be this way, or is a bug?

  • 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-28T03:39:56+00:00Added an answer on May 28, 2026 at 3:39 am

    persist() doesn’t insert the entity in database. And you’re not even guaranteed to have an ID assigned after persist() has been called.

    What persist does is to make a transient entity attached to the entity manager. At flush time, the version will be assigned.

    So, if you really need the new version before the transaction is committed, call flush() explicitely and then get the new version. But usually, it’s not needed because you don’t need to deal with the version field yourself, so just commit the transaction: it will cause a flush to be done, and the version will thus be assigned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which extends android.app.Application , which I use to persist global
I have set FlushModeType to COMMIT , but right after em.persist() call, the INSERT
I have some EJBs that use Hibernate to persist data to the database. I
I'd like to use object database to persist some classes created in IronPython. The
Use case: 3rd party application wants to programatically monitor a text file being generated
use this website a lot but first time posting. My program creates a number
Use case: I've just entered insert mode, and typed some text. Now I want
I have a one-to-one relation where I use CascadeType.PERSIST. This has over time build
I got these 2 entities: @javax.persistence.Entity public class Book { @javax.persistence.EmbeddedId private BookPK id;
I am now ready to use NHibernate to persist my data layer access. I

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.