One side annotated with OneToOne and the other with OneToMany annotation
-
14-10-2022 - |
Question
At first I thought it was a mistake but then I found this very odd situation in several other tutorials on the net. I don't understand how this works.
User bean:
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue
private Integer id;
private String login;
private String password;
@OneToOne(cascade=CascadeType.ALL)
@JoinTable(name="user_roles",
joinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}
)
private Role role;
Role bean:
@Entity
@Table(name="roles")
public class Role {
@Id
@GeneratedValue
private Integer id;
private String role;
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="user_roles",
joinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="user_id", referencedColumnName="id")}
)
private Set<user> userRoles;
Can someone please explain how this works? And why do it like this?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow