NHibernate条件查询帮助 [英] NHibernate criteria query help

查看:69
本文介绍了NHibernate条件查询帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于下表,我试图使用条件查询返回给定日期范围之间的给定资源的所有分配:

Given the following tables, I am trying to return all Allocations for a given Resource's that fall between a given range of dates using a criteria query:

create table Resources (
    ResourceId  integer,
   ResourceName TEXT not null,
   BusinessId TEXT not null,
   OrganizationName TEXT not null,
   primary key (ResourceId)
)

create table Allocations (
    AllocationId  integer,
   StartTime DATETIME not null,
   EndTime DATETIME not null,
   PostingTime DATETIME,
   ResourceId INTEGER,
   ActivityBaseId INTEGER,
   primary key (AllocationId),
  unique (StartTime, EndTime, ResourceId, ActivityBaseId)
)

public Resource FetchFor(Resource resource, DateRange range) {
    var allocations = _session.CreateCriteria<Resource>()
            .CreateCriteria("Allocations")
                .Add(Restrictions.Between("EndTime", (DateTime)range.Start, (DateTime)range.End))
            .List<Allocation>();

        if (allocations.Count() > 0) 
            resource.ReplaceAllocations(allocations);

        return resource;
    }

运行此命令时会出现此异常:

I get this exception when I run this:

failed: NHibernate.QueryException : could not resolve property: EndTime of: Domain.Model.Allocations.Allocation

该模型已映射,我可以毫无问题地保存资源及其关联的分配,因为我正在测试此Fetch查询。 NHib sql的示例行:
NHibernate:插入分配(StartTime,EndTime,PostingTime,ResourceId,ActivityBaseId)值(@ p0,@ p1,@ p2,@ p3,@ p4);选择last_insert_rowid(); @ p0 = 2010年1月28日12:00:00 AM,@ p1 = 2010年1月28日1:00:00 AM,@ p2 =空,@ p3 = 1,@ p4 = 4

The model is mapped and I can save a Resource and it's associated Allocations without trouble, as I am doing to test this Fetch query. An example line of NHib sql: NHibernate: INSERT INTO Allocations (StartTime, EndTime, PostingTime, ResourceId, ActivityBaseId) VALUES (@p0, @p1, @p2, @p3, @p4); select last_insert_rowid();@p0 = 1/28/2010 12:00:00 AM, @p1 = 1/28/2010 1:00:00 AM, @p2 = NULL, @p3 = 1, @p4 = 4

由于NHib清楚地了解如何映射Allocation.EndTime,所以异常消息令人困惑。请:

1)帮助解决问题/清理本文中的查询,并且

2)指出用于学习nhib查询的任何喜欢的资源,尤其是那里有示例时。

The exception message is confusing since NHib does clearly understand how to map Allocation.EndTime. Please:
1) help trouble shoot / clean up the query in this post, and
2) point out any favorite resources for learning nhib queries, especially if there are examples there.

顺便说一句,我的确意识到我没有在所示的示例中使用Resource参数,因为我几乎(mis?)在Ayende的< a href = http://ayende.com/Blog/archive/2009/05/19/nhibernate-queries-examples.aspx rel = nofollow noreferrer>发布。如果我可以使此联接查询正常工作,则将其转换为性能的子查询。

As an aside I do realize I am not using the Resource param in the example shown, as I pretty much (mis?)applied the code from an example on Ayende's posting. If I can get this join query working I will turn it into a subquery for performance.

谢谢!

Berryl

Thanks!
Berryl

=== NHib映射=====

=== NHib Mapping =====

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property"   auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Domain.Model.Allocations.Allocation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Allocations">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
  <column name="AllocationId" />
  <generator class="identity" />
</id>
<property name="TimeRange" type="Data.UserTypes.AllocationTimeRangeUserType, Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
  <column name="StartTime" not-null="true" unique-key="DomainSignature" />
  <column name="EndTime" not-null="true" unique-key="DomainSignature" />
</property>
<property name="PostingTime" type="System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="PostingTime" not-null="false" />
</property>
<many-to-one class="Domain.Model.Resources.Resource, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" foreign-key="Resource_FK" name="Resource">
  <column name="ResourceId" unique-key="DomainSignature" />
</many-to-one>
<many-to-one class="Domain.Model.Activities.ActivityBase, Smack.ConstructionAdmin.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" foreign-key="ActivityBase_FK" name="Activity">
  <column name="ActivityBaseId" unique-key="DomainSignature" />
</many-to-one>


推荐答案

似乎您没有名为EndTime的Allocation映射属性,因此出现此错误也就不足为奇了。 EndTime列通过自定义类型TimeRange进行映射,因此您希望对此进行查询。

It seems like you don't have a mapped property of Allocation called EndTime so its not surprising you get this error. The EndTime column is mapped through the custom type TimeRange so you would expect to query on that.

Allocation.cs和客户TimeRange的示例也可能有助于理解问题。

A sample of Allocation.cs and the customer TimeRange would probably also help understand the problem.

这篇关于NHibernate条件查询帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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