User Entity
class User{
int id;
@OneToMany
Set<Role> roles;
}
:User class have so many other details which i have not written.
DTO
class DTO{
int id;
Set<Role> roles;
DTO(int id, Set<Role> roles){
this.id = id;
this.roles= roles;
}
}
Query
hibernateTemplate.find("select new DTO(u.id, r ) from "+User.class.getName()+ " u inner join u.roles as r");
Issue : throws not a valid constructor found.
With below constructor modification the above query works:
DTO(int id, Role role){
this.id = id;
}
Issue: But now it gives multiple DTO records for same user equal to no of roles that user is having. Please help.
Since you need multiple rows to create a single DTO instance, you can’t use the new operator inside the query. Instead, you’ll have to create your DTOs by yourself. Something like this should do: