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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:05:47+00:00 2026-05-14T19:05:47+00:00

I want the first to be generated: @Id @Column(name = PRODUCT_ID, unique = true,

  • 0

I want the first to be generated:

@Id
@Column(name = "PRODUCT_ID", unique = true, nullable = false, precision = 12, 
        scale = 0)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PROD_GEN")
@BusinessKey
public Long getAId() {
    return this.aId;
}

I want the bId to be initially exactly as the aId. One approach is to insert the entity, then get the aId generated by the DB (2nd query) and then update the entity, setting the bId to be equal to aId (3rd query). Is there a way to get the bId to get the same generated value as aId?

Note that afterwards, I want to be able to update bId from my gui.

If the solution is JPA, even better.

  • 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-14T19:05:48+00:00Added an answer on May 14, 2026 at 7:05 pm

    Choose your poison:

    Option #1

    you could annotate bId as org.hibernate.annotations.Generated and use a database trigger on insert (I’m assuming the nextval has already been assigned to AID so we’ll assign the curval to BID):

    CREATE OR REPLACE TRIGGER  "MY_TRIGGER"
      before insert on "MYENTITY"
      for each row 
    begin  
      select "MYENTITY_SEQ".curval into :NEW.BID from dual;
    end; 
    

    I’m not a big fan of triggers and things that happen behind the scene but this seems to be the easiest option (not the best one for portability though).

    Option #2

    Create a new entity, persist it, flush the entity manager to get the id assigned, set the aId on bId, merge the entity.

    em.getTransaction().begin();
    MyEntity e = new MyEntity();
    ...
    em.persist(e);
    em.flush();
    e.setBId(e.getAId());
    em.merge(e);
    ...
    em.getTransaction().commit();
    

    Ugly, but it works.

    Option #3

    Use callback annotations to set the bId in-memory (until it gets written to the database):

    @PostPersist
    @PostLoad
    public void initialiazeBId() {
        if (this.bId == null) {
            this.bId = aId;
        }
    }
    

    This should work if you don’t need the id to be written on insert (but in that case, see Option #4).

    Option #4

    You could actually add some logic in the getter of bId instead of using callbacks:

    public Long getBId() {
        if (this.bId == null) {
            return this.aId;
        }
        return this.bId;
    }
    

    Again, this will work if you don’t need the id to be persisted in the database on insert.

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

Sidebar

Related Questions

I want to run a query on an Oracle database and for each column
First off, we are using JPA 2.0 and Hibernate 3.5 as persistence provider on
I have a text file that is automatically generated by an older computer system
I have a csv file with first line as fields and remaining lines as
I have data generated in a simulation. The generated data file looks something like
The question I'm about to ask is just because I'm curious. I think I'm
I am probably doing this the wrong way... but here's what I've got... it's
I'm having trouble getting a ModelUpdate or TryModelUpdate to work in my code. I'm
I hope anyone can help. I have to develop up against this third party
I'm a hibernate newbie and I'm not entirely sure how to get the cascade

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.