Ekaterina
Ekaterina

Reputation: 1892

Unable to generate UUID id with data.sql

Without data.sql everything works.

I'm using Spring Boot 2 and H2 database.

But when I'm trying to initialize the author table using data.sql I'm getting the error:

Caused by: org.h2.jdbc.JdbcSQLException: 
NULL not allowed for column "ID"; SQL statement: insert into author (`name`) values ('Pushkin')

Here is the Author entity:

@Entity
public class Author {
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
    private UUID id;
    @Column(unique=true)
    private String name;

    //getters/setters/constructors
}

Here is data.sql:

insert into author (`name`) values ('Pushkin');

Upvotes: 1

Views: 1798

Answers (2)

Ashish Dwivedi
Ashish Dwivedi

Reputation: 51

first of all please check that you have tick auto increment in database for id which want to generate and then write below in @ID, @GeneratedValue(strategy = GenerationType.IDENTITY). it will make your id unique.

Upvotes: 0

Simon Martinelli
Simon Martinelli

Reputation: 36223

The UUIDGenerator only works when you store entities with JPA/Hibernate.

When you do plain SQL in data.sql Hibernate is not used. You have to follow the suggestion of JB Nizet http://www.h2database.com/html/functions.html#random_uuid

Upvotes: 2

Related Questions