简体   繁体   English

如何将查询映射到非实体类+实体类

[英]How to map query to non-entity class + entity class

I know how to do query to resultClass mapping in IBatis. 我知道如何在IBatis中查询resultClass映射。

How can I map the native query result to the object that is a mix of entity class and scalars in hibernate ? 如何将本机查询结果映射到hibernate中混合了实体类和标量的对象? How can I set the parameters ? 如何设置参数?

Please Help. 请帮忙。

With Hibernate Session API, you can do it by comining addEntity() and addScalar() methods: 使用Hibernate Session API,您可以通过组合addEntity()addScalar()方法来实现:

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 : 在JPA中,您可以使用@SqlResultSetMapping

@SqlResultSetMappings(
    @SqlResultSetMapping(name = "projectWithCount"
        entities = @EntityResult(entityClass = Project.class),
        columns = @ColumnResult(name = "c")))

...

Query q = s.createSQLQuery(
        "...", "projectWithCount")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM