Hibernate: Generate an ID that starts with 100 instead of 1

Due to issues I’m seeing with Angular, two form fields cannot have the same ID.  The list of skills already has an ID that starts at 1, so I want the session id to start with 100.

To get this to work in the development MySQL database:

I needed to alter the Sessions table to start the auto increment at 100.  The script can be run after the table creation script is executed on the database.

ALTER TABLE Sessions AUTO_INCREMENT=100

To get this to work in the H2 in-memory database:

Spring Boot provides great support for interacting with SQL databases with minimal or no XML configurations. We need not add any configurations to connect to this embedded db, its all managed by Springboot. These embedded DBs are in-memory and each time the application shuts down the schema and data gets erased. One way to keep schema and data in the in-memory is to populate it during application startup. This is taken care by Springboot.

JPA and Spring Data with Spring Boot

This is achieved using Java Persistance API (JPA). Spring Data provides an excellent mechanism to achieve the persistence using JPA.

CREATE SEQUENCE session_id_seq START WITH 100 INCREMENT BY 1;
ALTER TABLE session ADD COLUMN id numeric DEFAULT nextval(‘session_id_seq’);
ALTER SEQUENCE session_id_seq OWNED BY session.id;

The above code can be found in schema-hsqldb.sql

You will then add this to the Java Entity class:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

 

 

Leave a Reply