I have a databse with users and their passwords. when i launch my app i create some users with roles and store them into database. My user entity:
@Entity
@Table(name = "USERS")
public class User {
@Id
@Column(name = "USERNAME", nullable = false, unique = true)
private String username;
@Column(name = "PASSWORD", nullable = false)
private String password;
@Column(name = "ENABLED", nullable = false)
private boolean enabled = true;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER)
private Set<UserRole> userRole = new HashSet<>();
...getters and setters
and userRole entity
@Entity
@Table(name = "user_roles")
public class UserRole {
@Id
@Column(name = "ROLE")
private String role;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "USERNAME")
private User user;
column role is unique=false by default, but when i add userA with roles: admin,user and then userB with roles: user, record userB - user overwrites record userA - user. Can someone point what is my mistake?
i'm using hibernate 5 + spring 5
An ID is, by definition, unique. That's what uniquely identifies an entity.