I'm working on a simple webapplication with Spring Boot. I use JPA with embedded (local) H2 database.
When I start the project, Hibernate makes all the tables and connection automatically. It works fine.
In the beginning I use the h2 built-in console to run SQL queries manually for testing.
Here are my related files:
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
application.yml (First time ddl-auto: create, then update):
spring:
jpa:
open-in-view: false
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.H2Dialect
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:file:~/Documents/database/blog
username: hazazs
password: {password}
h2:
console:
enabled: true
path: /database
Blog.java
@Entity(name = "BLOGS")
public class Blog implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date posted;
@Column(columnDefinition = "TEXT", nullable = false)
private String content;
@ManyToOne
private Blogger blogger;
Blogger.java
@Entity(name = "BLOGGERS")
public class Blogger implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private boolean gender;
@Column(unique = true, nullable = false)
private String username;
@ManyToMany
@JoinTable(
name = "BLOGGERS_ROLES",
joinColumns = {@JoinColumn(name = "BLOGGER_ID")},
inverseJoinColumns = {@JoinColumn(name = "ROLE_ID")}
)
private Set<Role> roles = new HashSet<>();
@OneToMany(mappedBy = "blogger", fetch = FetchType.EAGER)
private Set<Blog> blogs = new HashSet<>();
@Column(unique = true, nullable = false)
private String code;
@Column(nullable = false)
private boolean enabled;
Role.java
@Entity(name = "ROLES")
public class Role implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String name;
@ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER)
private Set<Blogger> bloggers = new HashSet<>();
With the first application run the ID increments automatically by 1. But then I restart my application and connect the database again, however it increments by 1 as well, but it starts with 32 all the time.
So if I run the query INSERT INTO ROLES (NAME) VALUES ('USER')
first time manually in the h2 console, it inserts USER with ID 1 correctly.
But if I run the query INSERT INTO ROLES (NAME) VALUES ('ADMIN')
after the application restart, it inserts ADMIN with ID 33 instead of 2.
The third run starts with 65 and so on.
I have tried so far (with no result):
-the 4 GenerationType for @GeneratedValue
-<scope>runtime</scope>
in the h2 dependency
-database url: url: jdbc:h2:file:~/Documents/database/blog;DB_CLOSE_ON_EXIT=FALSE
How could I achieve, that the ID continues from where it finished at the previous application run?