javadatabasehibernatesqlitejpa

Hibernate won't generate SQLite table with autoincrement PK


I created the JPA entity Country for a Springboot project

@Getter
@Setter
@Entity
@Data
public class Country {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String region;

    @Column(nullable = false)
    private String continent;
}

I ran the application and the SQLite database file was generated.

However, upon inspection using DB Browser for SQLite, I found out that the country table did not have AUTOINCREMENT in its schema.

The following is the generated DDL query for country table:

CREATE TABLE "country" (
    "id"    INTEGER,
    "continent" varchar(255) NOT NULL,
    "name"  varchar(255) NOT NULL,
    "region"    varchar(255) NOT NULL,
    PRIMARY KEY("id")
);

This is my application properties

spring.datasource.url=jdbc:sqlite:./database.sqlite
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true

How do I ensure that the schema of the generated table have AUTOINCREMENT?


Solution

  • SQLite will auto-increment the primary key if it's declared as INTEGER PRIMARY KEY, even without the keyword AUTOINCREMENT.

    References: