I have code that uses Hibernate to write to an Oracle table. It uses a SequenceGenerator to generate unique id's. Say I have id's 1 through 40 in the database. What happens is that if any users are deleted from the table, it leaves a gap (say, id=24) in the id's in the table. Then, when a new user is created, the new user's id is set by Hibernate to 24.
Now there is a problem because the immediate next new user gets an id=25, which causes a UniqueConstraint exception.
Is there something I'm doing wrong? How do I make Hibernate stop generating sequence values that already exist in the table?
@Entity
@Table(name="User")
public class User {
@Id
@SequenceGenerator(name="UserGen", sequenceName="UserSeq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="UserGen")
private Integer id;
@Column(length=64, unique=true)
private String username;
...
Here is the sequence info in Oracle:
CREATED 31-OCT-16
LAST_DDL_TIME 31-OCT-16
SEQUENCE_OWNER USERSERVICE
SEQUENCE_NAME USERSEQ
MIN_VALUE 1
MAX_VALUE 9999999999999999999999999999
INCREMENT_BY 1
CYCLE_FLAG N
ORDER_FLAG N
CACHE_SIZE 20
LAST_NUMBER 81
PARTITION_COUNT
SESSION_FLAG N
KEEP_VALUE N