0

I would like to execute a function every x time to update some values ​​in the database, without changing the ids of the existing ones. It is possible that values ​​that I already have and that I would not like to duplicate can be vegetated.

What I want to achieve is not to duplicate the values ​​or have to delete them (I want to keep the id)

@Entity
@EntityListeners(AuditingEntityListener::class)
@SQLInsert(sql="INSERT IGNORE INTO countries(country_code) VALUES(?)")
@Table(name = "countries")
class Country (
        @Column(unique=true)
        var countryCode: String? = null,
){
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long = 0
}

This is the error that I get when I do a saveAll and some value is duplicated

Caused by: org.hibernate.HibernateException: The database returned no natively generated identity value

I have tried, without the @SQLInsert and without the countryCode being unique

Alvaro
  • 1
  • 1

1 Answers1

0

I think you can find your answer here, you can fix it in two ways. The first solution is to check if is there already a record in the database with the given id with fintOneById(Integer id), if it is presented in DB just update values and call the save method. The second solution is to use a custom update with the @Modify query which will be triggered without any check from the database. You have more information on the shared link.