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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:11:44+00:00 2026-06-08T18:11:44+00:00

I user JPA 2.0 with eclipselink with postgres DB. I want to presist new

  • 0

I user JPA 2.0 with eclipselink with postgres DB.

I want to presist new entity Class. The primary key is an Integer, with not null constraint.

When i one to insert, JPA don’t know the value of the new Primary key.

How can i get the new primary key value?

I can’t change the DB sheme.

I think about get max id and insert a greater one to the DB, but it kind of messy solution.

This is the definition of the primary key in the entity class, generated with netBeans 7.0.1:
@Id
@Basic(optional = false)
@Column(name = “obj_id”)
private Integer ObjId;

    public void save(){
    TkTopoVerzio test= new TkTest();
    // set all the value
    // ....

    try {
            em.getTransaction().begin();
            em.persist(test);
            em.getTransaction().commit();  

     } catch(javax.persistence.NoResultException ex){
        //Todo : manage exception
     } catch (Exception ex){
         // toDO : manage exception 
     }

}

  • 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-06-08T18:11:45+00:00Added an answer on June 8, 2026 at 6:11 pm

    If this is an existing app, how does it assign primary keys?
    You can of course assign the pk in your new objects directly before calling persist, but EclipseLink also allows custom pk generators:
    http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing
    which would allow you to assign a pk using what ever strategy you need.

    The problem with what you are suggesting (using max to find existing values) is that this very difficult to be thread or even multi-app safe. A highly concurrent or distributed app may run into problems with pk reuse. UUID might be a better way to go if you cannot use a database oriented pk generation strategy. A good resource is here:
    http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Advanced_Sequencing

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

Sidebar

Related Questions

We have an JPA @Entity class (say User) which has a @ManyToOne reference (say
I have mapped class in JPA. : @Entity @Inheritance(strategy = InheritanceType.JOINED) public class User
I have a domain object class User (it is a JPA entity): @Entity public
I have a JPA domain entity that I'm updating from user input. Depending on
I am fairly new to JPA and want to find best practices when handling
I'm using eclipselink as JPA Implementation and PostgreSQL and want to store SHA-1 Hashes
I am a first time user of the new JPA 2.0 Criteria API and
I am using JPA 2.0 and hibernate. I have a User class and a
I am using JPA 2.0. (EclipseLink 2.0.2) If an entity contains relations, for example:
Today i have new problem with JPA/EJB3. I have 2 table User and Group

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.