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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:36:53+00:00 2026-05-25T23:36:53+00:00

Hi I have a domain object lets say Student and its Roll no as

  • 0

Hi I have a domain object lets say Student and its Roll no as a primary key

here is the sample mapping for it.

    @Id
@Column(name = "Roll_NO", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Roll_NO_SEQ")
@SequenceGenerator(name = "Roll_NO_SEQ", sequenceName = "Roll_NO_SEQ", allocationSize = 1)
public Long getRollNo() {
    return this.rollNo;
}

issue : lets say if a particular student was deleted from the database, and then re-admitted at the time of re-admission i want to retain the old roll no . so when i call session.save hibernate assigns a new roll No based on the Sequence specified rather then what i am setting through setRollNo() method. is there any way to achieve this in hibernate?

  • 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-25T23:36:54+00:00Added an answer on May 25, 2026 at 11:36 pm

    Given that you cannot change the legacy code, Ryan has the right idea. I had to do basically the same thing some time ago in a personal project. There were two parts: the easy part is to allow the effective setting of an otherwise-autonumber-ed column’s ID…and the other is to make the ID generator stop overwriting that value when you go to Save().

    Here’s the code to the FlexibleIDGenerator I used:

    public class FlexibleIDGenerator extends IdentityGenerator implements Configurable {
       public static final String DEFAULT = "default";
    
       private IdentifierGenerator assignedGenerator;
       private IdentifierGenerator defaultGenerator;
    
    @SuppressWarnings("unchecked")
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
          //boolean useDefault = false;
    
          if (object instanceof OverridableIdentity) {
              if (((OverridableIdentity) object).isIDOverridden()) {
                  try {
                      Class cl = object.getClass().getSuperclass();
                      Method[] methods = cl.getDeclaredMethods();
    
                      for (int i = 0; i < methods.length; i++) {
                          if (methods[i].getName().equalsIgnoreCase("setId")) {
                              methods[i].invoke(object, Integer.valueOf((((OverridableIdentity) object).getOverriddenID())));
                          }
                      }
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }
                  return assignedGenerator.generate(session, object);
              } else {
                  return defaultGenerator.generate(session, object);
              }
          } else {
              return defaultGenerator.generate(session, object);
          }
       }
    
       public void configure(Type type, Properties params, Dialect d) throws MappingException {
          assignedGenerator = IdentifierGeneratorFactory.create("assigned", type, params, d);
          defaultGenerator = IdentifierGeneratorFactory.create("increment", type, params, d);          
       }
    }
    

    To use that for a class, you update your Hibernate mapping file like this:

            <id
            name="Id"
            type="integer"
            column="id"
        >
            <generator class="com.mypackage.FlexibleIDGenerator"/>
        </id>
    

    One other detail: I added a method to my base object called “GetOverriddenID()” to avoid confusion about whether I’m using the “normal” ID (in Update() calls) or the overridden ones.

    Hope that helps.

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

Sidebar

Related Questions

I have this huge domain object(say parent) which contains other domain objects. It takes
I have a domain object annotated like this for hibernate support. @Entity @Table(name =
Lets say I have some domain objects that will need to be serialized/packed using
Let's say I have a Domain Object like this: public class Product { public
Let's say I have an interface representing a domain object: public interface IFoo {
Let's say I have a domain object that looks like this: @Entity @Indexed public
So let's say we have a domain object such as the following public class
Let's say my domain looks like this: I have an object, Vehicle, that has
Lets say I have a page on the same domain that I put inside
I have a domain object that has been auto generated for me by MyGeneration.

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.