Spring JPA Hibernate many to many insertion duplicate entry for primary key

Sachin Titus :
    2020-02-29 13:01:14.008 DEBUG 5200 --- [nio-8083-exec-2] org.hibernate.SQL                        : insert into users_roles (role_id, user_id) values (?, ?)
2020-02-29 13:01:14.009 TRACE 5200 --- [nio-8083-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [2]
2020-02-29 13:01:14.009 TRACE 5200 --- [nio-8083-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [5]
2020-02-29 13:01:14.010 DEBUG 5200 --- [nio-8083-exec-2] org.hibernate.SQL                        : insert into users_roles (user_id, role_id) values (?, ?)
2020-02-29 13:01:14.010 TRACE 5200 --- [nio-8083-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [5]
2020-02-29 13:01:14.010 TRACE 5200 --- [nio-8083-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [2]
2020-02-29 13:01:14.025  WARN 5200 --- [nio-8083-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1062, SQLState: 23000
2020-02-29 13:01:14.025 ERROR 5200 --- [nio-8083-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Duplicate entry '5-2' for key 'PRIMARY'
2020-02-29 13:01:14.027  INFO 5200 --- [nio-8083-exec-2] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2020-02-29 13:01:14.072  WARN 5200 --- [nio-8083-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement]

Authorities list inside USER entity

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinTable(name = "users_roles",
        joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id"))
@JsonIgnore
private Set<Authority> authorities = new HashSet<>();

users List inside Authorities entity

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.LAZY)
@JoinTable(name = "users_roles",
        joinColumns = @JoinColumn(name = "role_id"),
        inverseJoinColumns = @JoinColumn(name = "user_id"))
private Set<User> users = new HashSet<>();

I understand that when I save a USER entity, the AUTHORITY entity inside also gets saved, which again saves USER entity and that's what causes the trouble. How can I get around this? Thanks in advance!

Shababb Karim :

The problem you are probably facing is that since you have @JoinTable declaration on both entities so you have two insertions with the same composite primary keys in the Join Table. You can just remove the annotation from Authority class to this:

@ManyToMany(mappedBy="authorities")
private Set<User> users = new HashSet<>();

Keep everything else the same. Hopefully this will work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16486&siteId=1