Is this possible: JPA/Hibernate query with list property in result ?
Posted
by Kdeveloper
on Stack Overflow
See other posts from Stack Overflow
or by Kdeveloper
Published on 2010-04-20T21:07:17Z
Indexed on
2010/04/20
21:43 UTC
Read the original article
Hit count: 250
In hibernate I want to run this JPQL / HQL query:
select new org.test.userDTO( u.id, u.name, u.securityRoles)
FROM User u
WHERE u.name = :name
userDTO class:
public class UserDTO {
private Integer id;
private String name;
private List<SecurityRole> securityRoles;
public UserDTO(Integer id, String name, List<SecurityRole> securityRoles) {
this.id = id;
this.name = name;
this.securityRoles = securityRoles;
}
...getters and setters...
}
User Entity:
@Entity
public class User {
@id
private Integer id;
private String name;
@ManyToMany
@JoinTable(name = "user_has_role",
joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = {@JoinColumn(name = "security_role_id") }
)
private List<SecurityRole> securityRoles;
...getters and setters...
}
But when Hibernate 3.5 (JPA 2) starts I get this error:
org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate
constructor on class [org.test.UserDTO] [SELECT NEW org.test.UserDTO (u.id,
u.name, u.securityRoles) FROM nl.test.User u WHERE u.name = :name ]
Is a select that includes a list as a result not possible? Should I just create 2 seperate queries?
© Stack Overflow or respective owner