用户实体
class User{
int id;
@OneToMany
Set<Role> roles;
}
:用户类有很多我没有写的其他细节。
DTO
class DTO{
int id;
Set<Role> roles;
DTO(int id, Set<Role> roles){
this.id = id;
this.roles= roles;
}
}
询问
hibernateTemplate.find("select new DTO(u.id, r ) from "+User.class.getName()+ " u inner join u.roles as r");
问题:抛出找不到有效的构造函数。
通过以下构造函数修改,上述查询有效:
DTO(int id, Role role){
this.id = id;
}
问题:但现在它为同一用户提供了多个 DTO 记录,等于该用户拥有的角色数量。请帮忙。