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

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

问题描述

是否有可能使用投影查询返回的实体?

我已经成功地与一个SQL查询做了(见下文),但无法找到如何使用投影查询,做到这一点。

 昏暗的SQL作为字符串=SELECT {A *},{B *}从LEFT OUTER JOIN B上的a.pk = b.fk)

SQL结果转换为实体{A}和{B}
昏暗的查询作为IQUERY = session.CreateSQLQuery(SQL)_
                                  .AddEntity(一,的GetType(一))_
                                  .AddEntity(b的,的GetType(b))的

返回query.List()
 

解决方案

是的,你可以从投影查询返回的实体。

如果您使用的是HQL查询,你可以指定一个构造函数在HQL指定类选择条款:

 的IList<富> FOOS = session.CreateQuery(
    选择新的Foo(f.Name,f.Value)从富F)
    .LIST<富>();
 

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

您也可以使用Alias​​ToBean使用ResultTransformer,该自动映射从查询给定类型的属性返回的值。这种方法要求在查询中使用地图别名直接向给定类型的属性。例如:

 的IList<富> FOOS = session.CreateQuery(
    选择f.Name作为名称,f.Value的价值从富F)
    .SetResultTransformer(Transformers.Alias​​ToBean<富>())
    .LIST<富>();
 

为使这些示例工作,Foo类可能是这样的:

 公共类Foo
{
    公共美孚(){}

    公共美孚(字符串名称,双击值)
    {
        名称=名称;
        值=价值;
    }

    公共虚拟字符串名称{;组; }
    公共虚拟双值{获得;组; }
}
 

类上面包含第一个HQL例如一个有效的构造。它还定义了与在第二HQL查询中使用的别名对齐,这使得它可以为Alias​​ToBean变压器以填充从所述查询的结果的Foo类型实体公共属性。

不过,从你给的例子,它好像要在同一投影查询返回两种类型的实体。这可能是难以实现使用这些方法。我做了几个快速测试,没有任何的运气。

更新:

您可以使用Alias​​ToBean结果变压器的标准API以及与HQL。变压器用于以同样的方式,但投影查询看起来有点不同的使用条件。下面是一个使用条件查询的例子:

 的IList<富> FOOS = session.CreateCriteria<富>()
    .SetProjection(Projections.ProjectionList()
        。新增(Projections.Property(姓名),姓名)
        。新增(Projections.Property(值),值))
    .SetResultTransformer(Transformers.Alias​​ToBean<富>())
    .LIST<富>();
 

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

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.

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>();

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

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>();

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; }
}

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.

Update:

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天全站免登陆