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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:37:53+00:00 2026-05-20T08:37:53+00:00

I’m using before and after insert triggers to generate ids (primary key) of the

  • 0

I’m using before and after insert triggers to generate ids (primary key) of the form “ID_NAME-000001” in several tables. At the moment, the value of the hibernate generator class of these pojos is assigned. A random string is assigned to the object to be persisted and when it’s inserted by hibernate, the trigger assigns a correct id value.

The problem with this approach is that I’m unable to retrieve the persisted object because the id only exists in the database, not in the object I just saved.

I guess I need to create a custom generator class that could retrieve the id value assigned by the trigger. I’ve seen an example of this for oracle (https://forum.hibernate.org/viewtopic.php?f=1&t=973262) but I haven’t been able to create something similar for MySQL. Any ideas?

Thanks,

update:

Seems that this is a common and, yet, not solved problem. I ended up creating a new column to serve as a unique key to use a select generator class.

  • 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-20T08:37:53+00:00Added an answer on May 20, 2026 at 8:37 am

    Hope this won’t spark a holy war for whether using surrogate key or not. But it’s time to open the conversation here.

    Another approach would be just, use the generated key as surrogate key and assign a new field for your trigger assigned id. The surrogate key is the primary key. You have the logically named key (such as the “ID_NAME-000001” in your example). So your database rows will have 2 keys, the primary key is surrogate key (could be UUID, GUID, running number).
    Usually this approach is preferable, because it can adapt to new changes better.
    Say, you have these row using surrogate key instead of using the generated id as natural key.

    Surrogate key:

    id: "2FE6E772-CDD7-4ACD-9506-04670D57AA7F", logical_id: "ID_NAME-000001", ...
    

    Natural key:

    id: "ID_NAME-000001", ...
    

    When later a new requirement need the logical_id to be editable, auditable (was it changed, who changed it when) or transferable, having the logical_id as primary key will put you in trouble. Usually you cannot change your primary key. It’s horribly disadvantage when you already have lots of data in your database and you have to migrate the data because of the new requirement.

    With surrogate key solution, it’ll be easy, you just need to add

    id: "2FE6E772-CDD7-4ACD-9506-04670D57AA7F", logical_id: "ID_NAME-000001", valid: "F", ...
    id: "0A33BF97-666A-494C-B37D-A3CE86D0A047", logical_id: "ID_NAME-000001", valid: "T", ...
    

    MySQL doesn’t support sequence (IMO autoincrement isn’t comparable to sequence). It’s different from Oracle/PostgreSQL’s sequence. I guess that’s the cause why it’s difficult to port the solution from Oracle database to MySQL. PostgeSQL does.

    • 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.