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?
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:
To use that for a class, you update your Hibernate mapping file like this:
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.