Home > Enterprise >  Primary Key Violation in spring boot version 2.7.0 (was working on 2.6.7)
Primary Key Violation in spring boot version 2.7.0 (was working on 2.6.7)

Time:05-25

I am getting PK violation (with initial data in data.sql) in spring boot and after investigation I found that if I downgrade the version in pom.xml from 2.7 into 2.6.7 then everything works. I am using H2 database

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.0</version> <!-- will not work -->
    <!-- <version>2.6.7</version>  will  work -->
    <relativePath/> <!-- lookup parent from repository -->
</parent>

and here is the entity

@MappedSuperclass
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;
}

What is the changes? what am I missing ?

CodePudding user response:

I have same problem.

My @GeneratedValue(strategy = GenerationType.IDENTITY) was working fine with Spring Boot 2.6.8 (defaults to H2 1.4.200).

When changing to Spring Boot 2.7.0 (which defaults to H2 2.1.212) it stopped working with the mentioned primary key violation.

I have been able to upgrade to Spring Boot 2.7.0 keeping the previous H2 version:

<properties>
   <h2.version>1.4.200</h2.version>
</properties>

CodePudding user response:

Same problem here,

I think that is not only a problem with H2 version because I was using H2 2.1.212 with spring boot 2.6.7 without any problem, the problem appears after upgrade to spring 2.7.0

Something that worked for me: I have sql initialization files at src/main/resources and at src/test/resources. If I remove the data.sql at src/main/resources the problem disapears. Something has changed in spring 2.7.0

  • Related