I have simple Relation User to Role

@OneToOne
@JoinColumn(name = "role_id")
private Role role;



@Entity(name = "role")
public class Role {

    @Id
    @GeneratedValue
    private long id;

    @Column(name = "name", length = 50, nullable = false)
    private String name;

after update of Hibernate from 6.1.7 to 6.2.2 I get following Error

15:51:37.348 [main] [ traceId:   spanId:   ]  INFO  [ork.data.jpa.repository.query.QueryEnhancerFactory:47.<clinit>()] - Hibernate is in classpath; If applicable, HQL parser will be used. 
15:51:39.427 [main] [ traceId:   spanId:   ]  WARN  [  org.hibernate.engine.jdbc.spi.SqlExceptionHelper:145.logExceptions()] - SQL Error: 1062, SQLState: 23000 
15:51:39.427 [main] [ traceId:   spanId:   ]  ERROR [  org.hibernate.engine.jdbc.spi.SqlExceptionHelper:150.logExceptions()] - Duplicate entry '1' for key 'user.UK_qleu8ddawkdltal07p8e6hgva' 
15:51:39.440 [main] [ traceId:   spanId:   ]  ERROR [             io.config.db.DataInitializer:213.init()] - Cannot initialize internal users. 
org.springframework.dao.DataIntegrityViolationException: could not execute statement [Duplicate entry '1' for key 'user.UK_qleu8ddawkdltal07p8e6hgva'] [insert into user (account_id,created,last_login,last_modified,locked,recover_verify_token,recover_verify_token_exp,role_id,status,id) values (?,?,?,?,?,?,?,?,?,?)]; SQL [insert into user (account_id,created,last_login,last_modified,role_id,status,id) values (?,?,?,?,?,?,?)]; constraint [user.UK_qleu8ddawkdltal07p8e6hgva]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:269)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:229)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:565)
....
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement [Duplicate entry '1' for key 'user.UK_qleu8ddawkdltal07p8e6hgva'] [insert into user (account_id,created,last_login,last_modified,role_id,status,id) values (?,?,?,?,?,?,?)]

In Migration guide I do not find some relevant information.

With version 6.1.7 is everything fine and mapping is working as expected.

Any ideas?

Thanks.

1

There are 1 best solutions below

3
Pavel Muravyov On

As pointed in the migration doc: link, you should change relation type in User entity from @OneToOne to @ManyToOne (and not adding @UniqueConstraint, unless you need it)

Before:

@OneToOne
@JoinColumn(name = "role_id")
private Role role;

After:

@ManyToOne
@JoinColumn(name = "role_id")
private Role role;