我有2个表User
和Role
。这两个表创建合并表。那么如何根据id
找到用户角色?
用户:
@Entity
public class User {
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty
private String username;
@Email
@Column(unique = true)
@NotEmpty
private String email;
@NotEmpty
private String firstname;
@NotEmpty
private String lastname;
@NotNull
private int age;
@Size(min = 4)
private String password;
@NotNull
private UserType usertype = UserType.user;
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "USER_ROLES", joinColumns = {
@JoinColumn(name = "USER_ID", referencedColumnName = "id") }, inverseJoinColumns = {
@JoinColumn(name = "ROLE_NAME", referencedColumnName = "name") })
private List<Role> roles;
角色:
@Entity
public class Role {
@Id
private String name;
@ManyToMany(mappedBy="roles")
private List<User> users;
这2个表正在创建user_roles
表。
通过UserRepository,我可以轻松访问用户的信息。
我想从user或user_roles学习用户角色。我怎样才能做到这一点。
还可以使用任何方法为user_roles
创建存储库吗?