How to map query to non-entity class + entity class
I know how to do query to resultClass mapping in IBati开发者_开发百科s.
How can I map the native query result to the object that is a mix of entity class and scalars in hibernate ? How can I set the parameters ?
Please Help.
With Hibernate Session
API, you can do it by comining addEntity()
and addScalar()
methods:
Query q = s.createSQLQuery(
"select p.*, count(e.id) as c " +
"from Project p left join Employee e on p.id = e.project_id " +
"group by p.id")
.addEntity(Project.class).addScalar("c");
In JPA you can do it with @SqlResultSetMapping
:
@SqlResultSetMappings(
@SqlResultSetMapping(name = "projectWithCount"
entities = @EntityResult(entityClass = Project.class),
columns = @ColumnResult(name = "c")))
...
Query q = s.createSQLQuery(
"...", "projectWithCount")
精彩评论