开发者

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜