我使用SpringDataJpa进行多表关联,其中几个实体类的对应情况如下:
User:
public class User extends BaseEntity {
private String username;
private String password;
@ManyToOne ( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
@JoinColumn ( name = "dept_id" )
private Department depts;
@ManyToOne ( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
@JoinColumn ( name = "role_id" )
private Role role;
}
Role :
public class Role extends BaseEntity {
private String role;
private String roleName;
@OneToMany (mappedBy = "roles",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private Set<User> users;
@ManyToMany (mappedBy = "roles",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(name = "t_role_route",joinColumns=@JoinColumn(name="role_id",referencedColumnName = "id"),inverseJoinColumns = @JoinColumn(name = "route_id"))
@JsonIgnore
private Set<Route> routes;
@ManyToOne ( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
@JoinColumn ( name = "dept_id" )
private Department depts;
}
Route:
public class Route extends BaseEntity {
private String route;
private String routeName;
@ManyToMany(mappedBy = "routes")
private Set<Role> roles;
}
Department:
public class Department extends BaseEntity {
private String deptName;
private String dept;
@OneToMany (mappedBy = "depts",cascade= CascadeType.ALL,fetch= FetchType.LAZY)
private Set<User> users;
@OneToMany (mappedBy = "depts",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private Set<Role> roles;
}
现在我在登录后想返回这么一个对象:
UserVo:
private String username;
private String password;
private List<String> routes;
private List<String> routeNames;
private String deptName;
private String dept;
private Collection<? extends GrantedAuthority> authorities;
其中routes
和routeNames
是role中routes的属性,deptName
和dept
是user中dept的属性,现在我想在用户登陆的时候根据username 从数据库查询到user以及关联的信息,并且使用mapStruct进行转换成userVO,请问这个该怎么转化呢?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…