使用nhibernate从DB表中选择部分数据

时间:2022-01-14 03:46:21

i have a complex entity, very very heavy. i want to select only the id and the name of this entity from the db for a better performance. how do i do that with nhibernate (or fluent nhibernate).

我有一个复杂的实体,非常沉重。我想从数据库中仅选择此实体的ID和名称以获得更好的性能。我如何用nhibernate(或流利的nhibernate)做到这一点。

2 个解决方案

#1


You have 2 choices :

你有2个选择:

when using ICriteria - use (syntax might not be correct but you get the picture)

使用ICriteria时 - 使用(语法可能不正确,但你得到的图片)

SetProjections(Projections.ProjectionList.Add(Projections.Property("prop"))
                                     .Add(Projections.Property("prop1")))

when using hql

什么时候使用hql

select c.Col1,c.Col2 from table c

Both of them will return a list of array lists - or something like that - you then have to traverse it and build your .. dictionary (or whatever you have).

它们都将返回一个数组列表列表 - 或类似的东西 - 然后你必须遍历它并构建你的..字典(或任何你有的)。

#2


There are a few different possibilities.

有几种不同的可能性。

Create a New Entity

创建一个新实体

One possible solution is to create a new entity and map it to the same table, but only map the columns that you want (id and name). This is quite flexible and lets you use that entity as any other entity. The problem is that you introduce some duplication.

一种可能的解决方案是创建一个新实体并将其映射到同一个表,但只映射您想要的列(id和name)。这非常灵活,允许您将该实体用作任何其他实体。问题是你引入了一些重复。

Using HQL

Another solution is to use projections. With HQL you can use the select clause to specify which columns to retrieve. If you want a proper entity instance as the result from the query and not an array of objects, you can create a custom constructor for your class and use that in the HQL query.

另一种解决方案是使用投影。使用HQL,您可以使用select子句指定要检索的列。如果您希望将适当的实体实例作为查询的结果而不是对象数组,则可以为类创建自定义构造函数并在HQL查询中使用它。

session.CreateQuery("select new Foo(f.Id, f.Name) from Foo f").List<Foo>();

Using the Criteria API

使用Criteria API

If you want to use the Criteria API instead of HQL, you can use the SetProjection method. If you want a proper entity from the query and not an array of objects, you can use the AliasToBean result transformer.

如果要使用Criteria API而不是HQL,可以使用SetProjection方法。如果您想从查询中获取正确的实体而不是对象数组,则可以使用AliasToBean结果转换器。

session.CreateCriteria(typeof(Foo))
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Name"), "Name")
        .Add(Projections.Property("Id"), "Id"))
    .SetResultTransformer(Transformers.AliasToBean(typeof(Foo)))
    .List();

The criteria example code is borrowed from the following question, which might be of interest: NHibernate - Only retrieve specific columns when using Critera queries?

标准示例代码借用了以下问题,这可能是有趣的:NHibernate - 仅在使用Critera查询时检索特定列?

#1


You have 2 choices :

你有2个选择:

when using ICriteria - use (syntax might not be correct but you get the picture)

使用ICriteria时 - 使用(语法可能不正确,但你得到的图片)

SetProjections(Projections.ProjectionList.Add(Projections.Property("prop"))
                                     .Add(Projections.Property("prop1")))

when using hql

什么时候使用hql

select c.Col1,c.Col2 from table c

Both of them will return a list of array lists - or something like that - you then have to traverse it and build your .. dictionary (or whatever you have).

它们都将返回一个数组列表列表 - 或类似的东西 - 然后你必须遍历它并构建你的..字典(或任何你有的)。

#2


There are a few different possibilities.

有几种不同的可能性。

Create a New Entity

创建一个新实体

One possible solution is to create a new entity and map it to the same table, but only map the columns that you want (id and name). This is quite flexible and lets you use that entity as any other entity. The problem is that you introduce some duplication.

一种可能的解决方案是创建一个新实体并将其映射到同一个表,但只映射您想要的列(id和name)。这非常灵活,允许您将该实体用作任何其他实体。问题是你引入了一些重复。

Using HQL

Another solution is to use projections. With HQL you can use the select clause to specify which columns to retrieve. If you want a proper entity instance as the result from the query and not an array of objects, you can create a custom constructor for your class and use that in the HQL query.

另一种解决方案是使用投影。使用HQL,您可以使用select子句指定要检索的列。如果您希望将适当的实体实例作为查询的结果而不是对象数组,则可以为类创建自定义构造函数并在HQL查询中使用它。

session.CreateQuery("select new Foo(f.Id, f.Name) from Foo f").List<Foo>();

Using the Criteria API

使用Criteria API

If you want to use the Criteria API instead of HQL, you can use the SetProjection method. If you want a proper entity from the query and not an array of objects, you can use the AliasToBean result transformer.

如果要使用Criteria API而不是HQL,可以使用SetProjection方法。如果您想从查询中获取正确的实体而不是对象数组,则可以使用AliasToBean结果转换器。

session.CreateCriteria(typeof(Foo))
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Name"), "Name")
        .Add(Projections.Property("Id"), "Id"))
    .SetResultTransformer(Transformers.AliasToBean(typeof(Foo)))
    .List();

The criteria example code is borrowed from the following question, which might be of interest: NHibernate - Only retrieve specific columns when using Critera queries?

标准示例代码借用了以下问题,这可能是有趣的:NHibernate - 仅在使用Critera查询时检索特定列?