首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NHibernate条件查询帮助

NHibernate条件查询帮助
EN

Stack Overflow用户
提问于 2010-01-26 22:04:47
回答 2查看 2.2K关注 0票数 0

在给定以下表格的情况下,我尝试使用条件查询返回在给定日期范围内的给定资源的所有分配:

代码语言:javascript
复制
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;
    }

当我运行下面的代码时,我得到了这个异常:

代码语言:javascript
复制
failed: NHibernate.QueryException : could not resolve property: EndTime of: Domain.Model.Allocations.Allocation

模型被映射了,我可以毫不费力地保存一个资源及其相关的分配,就像我测试这个Fetch查询一样。插入分配(StartTime,EndTime,PostingTime,ResourceId,ActivityBaseId)值(@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查询的常用资源,特别是如果有示例的话。

顺便说一句,我意识到我没有在显示的示例中使用资源参数,因为我几乎(错误地)在Ayende的posting上应用了示例中的代码。如果我能让这个连接查询正常工作,我会把它变成一个子查询,以提高性能。

谢谢!

Berryl

=== NHib映射=====

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-01-28 02:25:03

您似乎没有一个名为EndTime的分配映射属性,因此出现此错误也就不足为奇了。EndTime列是通过自定义类型TimeRange映射的,因此您可以期望在该列上进行查询。

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

票数 1
EN

Stack Overflow用户

发布于 2010-01-27 21:28:32

我一直在尝试复制您的问题,但没有成功。由于您在查询中没有使用资源,您是否可以尝试简化操作,例如:

代码语言:javascript
复制
var allocations = _session.CreateCriteria<Allocation>()  
            .Add(Restrictions.Between("EndTime", (DateTime)range.Start, (DateTime)range.End))
        .List<Allocation>(); 

我已经做了一些连接查询,我可以提供下面的例子,我希望这会有帮助:

代码语言:javascript
复制
        public IList<ItemOrder> GetItemOrderByCriteria(int? itemNumber, int? warehouseNumber, DateTime? orderPickDate, string orderStoreNum, string statusCode)
    {
        try
        {
            NHibernate.ICriteria criteria = NHibernateSession.CreateCriteria(typeof(Core.SuggestedOrders.ItemOrder));

            if (itemNumber.HasValue)
                criteria.CreateCriteria("Item", "Item").Add(Expression.Eq("Item.ItemNumber", itemNumber.Value));

            if (warehouseNumber.HasValue)
                criteria.CreateCriteria("Warehouse", "Warehouse").Add(Expression.Eq("Warehouse.WarehouseNumber", warehouseNumber));

            if (!String.IsNullOrEmpty(orderStoreNum))
                criteria.Add(Expression.Eq("OrdStoreNum", orderStoreNum));

            if (!String.IsNullOrEmpty(statusCode))
                criteria.Add(Expression.Eq("StatusCode", statusCode));

            if (orderPickDate.HasValue)
            {
                DateTime minPickDate = new DateTime(orderPickDate.Value.Year, orderPickDate.Value.Month, orderPickDate.Value.Day, 0,0,0);
                DateTime maxPickDate = new DateTime(orderPickDate.Value.Year, orderPickDate.Value.Month, orderPickDate.Value.Day, 23,59,59);

                criteria.Add(Expression.Between("OrdPickDate", minPickDate, maxPickDate));
            }

            return criteria.List<Core.SuggestedOrders.ItemOrder>();
        }
        catch (NHibernate.HibernateException he)
        {
            DataAccessException dae = new DataAccessException("NHibernate Exception", he);
            throw dae;
        }

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2139846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档