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 6019507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:25:54+00:00 2026-05-23T03:25:54+00:00

I’m developing a J2EE 6 Web Application, using a MySql 5.02 DataBase. I’m trying

  • 0

I’m developing a J2EE 6 Web Application, using a MySql 5.02 DataBase. I’m trying to generate a hash digest of the ID, every time I create a new Entity. This is set to a column on the Table.

But well, I’m stuck on something that is apparently easy, and according with what I found googling, possible. Basically I want to retrieve the ID (Primary Key) of a newly persisted object, but whatever I try, it returns null.

The steps are follow are:

  1. Create the Instance of the Entity -> userCard = new Users();
  2. Setting the corresponding fields with some values.
  3. Calling the persist() method of the EntityManager.

After reading some forums, I understood I had to either call flush() after persist(), or use merge() and use the returned entity to retrieve the id.

public void createAndFlush(Users users) {
    em.persist(users);
    em.flush();
}

public Integer edit(Users users) {
    return ((Users)em.merge(users)).getIdvcards();
}

None of them (among some other combinations) work, well, the Entity is successfully persisted, but the ID field returns null.

These are the correspinding annotations of the id column I want to retrieve:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idvcards")
private Integer idvcards;

And obviously, the column idvcards of my table is set to Auto-Increment and Primary Key.

Maybe I just need to clarify my Peristence-Management concepts, but I’d appreciate if I can get some hint to solve this basic issue.

  • 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-23T03:25:55+00:00Added an answer on May 23, 2026 at 3:25 am

    Maybe is not the most elegant solution ever, but finally I succeded on retrieving the ID of the new Entity:

    public Integer create(User user) {
        em.persist(users);
        em.flush();
        return (Integer) em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(users);
    }
    

    And well, althought is not related to the functionality, I changed the entity name to the singular form as @Bohemian suggested.

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

Sidebar

Related Questions

No related questions found

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.