I have a User entity with a unidirectional one-to-many with join-column set of Roles like
@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="user_ref")
private Set<Role> roles;
However, when I remove a role from the set and persist the user, Hibernate insists on doing a pre-delete:
update role set user_ref=null where user_ref=?
which goes kaboom due to a database foreign key constraint. This behavior persist even if I in the Role do a
@Column(name = "user_ref", nullable = false)
private int userId;
(same for updatable=false)
Pointers appreciated, Nik
CodePudding user response:
As you are mapping a unidirectional one to many, you shouldn't have a userId in the role entity.
Also, there are better ways of mapping the relationship between User and Role that will prevent the need for these update statements such as bidirectional one to many or unidirectional many to one.