鉴于以下表格,我试图使用条件查询返回给定资源的所有分配:
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查询一样。 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 < / p>
异常消息令人困惑,因为NHib清楚地了解如何映射Allocation.EndTime。请:
1)帮助解决这篇文章中的查询/清理问题,以及
2)指出任何用于学习nhib查询的最喜欢的资源,特别是如果有例子的话。
顺便说一下,我确实意识到我没有在显示的示例中使用Resource param,因为我几乎(错误?)应用了Ayende的posting上的示例中的代码。如果我可以使这个连接查询工作,我会把它变成一个子查询的性能。
谢谢!
Berryl
=== 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>
答案 0 :(得分:1)
我一直试图复制你的问题,但没有成功。由于您没有在查询中使用资源,您是否可以尝试简化操作,例如:
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;
}
}
答案 1 :(得分:1)
好像你没有Allocation的映射属性叫做EndTime所以你得到这个错误并不奇怪。 EndTime列通过自定义类型TimeRange进行映射,因此您可以在此查询。
Allocation.cs示例和客户TimeRange可能也有助于理解问题。