在给定以下表格的情况下,我尝试使用条件查询返回在给定日期范围内的给定资源的所有分配:
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;
}当我运行下面的代码时,我得到了这个异常:
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映射=====
<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>发布于 2010-01-28 02:25:03
您似乎没有一个名为EndTime的分配映射属性,因此出现此错误也就不足为奇了。EndTime列是通过自定义类型TimeRange映射的,因此您可以期望在该列上进行查询。
Allocation.cs和客户TimeRange的示例也可能有助于理解问题。
发布于 2010-01-27 21:28:32
我一直在尝试复制您的问题,但没有成功。由于您在查询中没有使用资源,您是否可以尝试简化操作,例如:
var allocations = _session.CreateCriteria<Allocation>()
.Add(Restrictions.Between("EndTime", (DateTime)range.Start, (DateTime)range.End))
.List<Allocation>(); 我已经做了一些连接查询,我可以提供下面的例子,我希望这会有帮助:
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;
}
}https://stackoverflow.com/questions/2139846
复制相似问题