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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:52:45+00:00 2026-06-14T15:52:45+00:00

I have a table with an index auto filled by a trigger that use

  • 0

I have a table with an index auto filled by a trigger that use a sequence (Oracle database)

CREATE TABLE A
(
  IDS                           NUMBER(10)      NOT NULL
)


CREATE OR REPLACE TRIGGER A_TRG
BEFORE INSERT
ON A REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
  :new.IDS := A_SEQ.nextval;
END A_TRG;
/

I have a matching Java class:

Class A {
   @Id
   @SequenceGenerator(name = "aSequence", sequenceName = "A_SEQ", allocationSize = 1)
   @GeneratedValue(generator = "aSequence", strategy = GenerationType.SEQUENCE)
   @Column(name = "IDS")
   Long id;

   ...
}

When I try to persist an instance of A like this:

EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
A a = new A();
Long id = getHibernateTemplate().save(a);
transaction.commit();

I get this problem:

  • ID in code returned by the save call = “X”

  • ID in database = “X+1”

Is there a way to setup Hibernate to let the database trigger create the ID ?

Thanks

  • 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-14T15:52:47+00:00Added an answer on June 14, 2026 at 3:52 pm

    Reponse found at HIbernate issue with Oracle Trigger for generating id from a sequence

    I need to adapt my trigger to run only if no ID is given:

    CREATE OR REPLACE TRIGGER A_TRG
    BEFORE INSERT
    ON A REFERENCING NEW AS New OLD AS Old
    FOR EACH ROW
    WHEN (New.IDS is null) -- (1)
    BEGIN
      :new.IDS := A_SEQ.nextval;
    END A_TRG;
    /
    

    (1) this line allow Hibernate to manually call A_SEQ.nextVal to set the ID and then bypass the trigger else Hibernate will get the nextval for uselessly because the trigger will always reset the ID calling nextval again

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

Sidebar

Related Questions

If I have a table column with data and create an index on this
I have got a simple MySQL table and primary index (id) is not numbered
I have question about database optimizing, indexing. I have table that called projects and
I have table: CREATE TABLE IF NOT EXISTS `TxtDila` ( `ID` int(11) NOT NULL
I have a table looking like this: CREATE TABLE `item` ( `id` int(11) NOT
I have a table with 3 columns Column 0: auto inc index Column 1:
I have a table called users: CREATE TABLE `users` ( `UID` INT UNSIGNED NOT
I have a database table that stores imported information. For simplicity, its something like:
I have a table 'posts' in database which has non-unique index on user_id (Key:
I have a table with two specific columns, one is auto-incremented number, the other

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.