问题描述
如何将本地查询结果映射到实体类和标量组合的对象在休眠?如何设置参数?
请帮助。
使用Hibernate Session
API,您可以通过将 addEntity()
和 addScalar()
方法:
查询q = s.createSQLQuery(
select p。*, (p.id)作为c+
从项目p离开,并在p.id = e.project_id+
group by p.id中加入Employee e)
.addEntity (Project.class).addScalar( C);
在JPA中,您可以使用 @SqlResultSetMapping
:
pre code $ @SqlResultSetMappings(
@SqlResultSetMapping(name =projectWithCount
entities = @EntityResult( entityClass = Project.class),
columns = @ColumnResult(name =c)))
...
Query q = s.createSQLQuery(
...,projectWithCount)
I know how to do query to resultClass mapping in IBatis.
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")
这篇关于如何将查询映射到非实体类+实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!