本文介绍了如何将查询映射到非实体类+实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何将本地查询结果映射到实体类和标量组合的对象在休眠?如何设置参数?



请帮助。

解决方案

使用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")

这篇关于如何将查询映射到非实体类+实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 10:17