通过投影查询返回实体 [英] Return entity via projection query

查看:17
本文介绍了通过投影查询返回实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用投影查询返回实体?

Is it possible to return an entity using a projection query?

我已经使用 SQL 查询成功完成了它(见下文),但找不到如何使用投影查询来完成它.

I've successfully done it with a SQL query (see below), but can't find how to do it with a projection query.

Dim sql As String = "SELECT {a.*}, {b.*} FROM a LEFT OUTER JOIN b ON a.pk = b.fk")

' Convert SQL results into entities {a} and {b}
Dim query As IQuery = session.CreateSQLQuery(sql) _
                                  .AddEntity("a", GetType(a)) _
                                  .AddEntity("b", GetType(b))

Return query.List()

推荐答案

是的,您可以从投影查询中返回实体.

Yes, you can return entities from projection queries.

如果您使用的是 HQL 查询,您可以在 HQL select 子句中为给定的类指定一个构造函数:

If you are using a HQL query you can specify a constructor for the given class in the HQL select clause:

IList<Foo> foos = session.CreateQuery(
    "select new Foo(f.Name, f.Value) from Foo f")
    .List<Foo>();

此示例要求 Foo 类具有适合 HQL 查询中使用的签名的构造函数.即:

This example requires that the Foo class has a constructor that fits the signature used in the HQL query. I.e:

您还可以使用 AliasToBean ResultTransformer,它会自动将从查询返回的值映射到给定类型的属性.这种方法要求查询中使用的别名直接映射到给定类型的属性.例如:

You could also use the AliasToBean ResultTransformer, which automatically maps the values returned from the query to the properties of a given type. This approach requires that the aliases used in the query map directly to properties of the given type. For example:

IList<Foo> foos = session.CreateQuery(
    "select f.Name as Name, f.Value as Value from Foo f")
    .SetResultTransformer(Transformers.AliasToBean<Foo>())
    .List<Foo>();

为了使这些示例有效,Foo 类可能如下所示:

To make these examples work, the Foo class could look like this:

public class Foo
{
    public Foo() { }

    public Foo(string name, double value)
    {
        Name = name;
        Value = value;
    }

    public virtual string Name { get; set; }
    public virtual double Value { get; set; }
}

上面的类包含第一个 HQL 示例的有效构造函数.它还定义了与第二个 HQL 查询中使用的别名一致的公共属性,这使得 AliasToBean 转换器可以从查询结果中填充 Foo 类型的实体.

The class above contains a valid constructor for the first HQL example. It also defines public properties that align with the aliases used in the second HQL query, which makes it possible for the AliasToBean transformer to fill entities of the type Foo from the results of the query.

但是,从您提供的示例来看,您似乎想从同一个投影查询中返回两种类型的实体.使用这些方法可能更难实现.我做了几次快速测试,但没有任何运气.

However, from the example that you give, it seems as if you want to return two types of entities from the same projection query. That might be harder to achieve using these methods. I did a couple of quick tests, without any luck.

更新:

您可以将 AliasToBean 结果转换器与 Criteria API 以及 HQL 一起使用.转换器的使用方式相同,但使用 Criteria 的投影查询看起来有点不同.这是一个使用条件查询的示例:

You can use the AliasToBean result transformer with the Criteria API as well as with HQL. The transformer is used in the same way, but the projection query looks a bit different using Criteria. Here is an example that uses a criteria query:

IList<Foo> foos = session.CreateCriteria<Foo>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Name"), "Name")
        .Add(Projections.Property("Value"), "Value"))
    .SetResultTransformer(Transformers.AliasToBean<Foo>())
    .List<Foo>();

这篇关于通过投影查询返回实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆