为什么我的 HQL 查询可以工作,但 Criteria 版本却不能?
我的(流畅的)NHibernate 模式有点复杂。这里讨论的两个类都是特定于国家/地区的,并且继承自抽象类。这背后的要点是不同国家的数据存储在不同的表中。除了以下情况之外,一切都运行良好。这个 HQL 执行得很好:(
dbSession.CreateQuery(@"from DateBlock_US db where
db.HaveListing.City.LocationID = " + searchLocation.LocationID.ToString()).List<DateBlock>();
我知道我应该使用 SetParameter,但这只是一个演示)。然而,这个标准不起作用:
dbSession.CreateCriteria(typeof(DateBlock_US))
.Add(Restrictions.Eq("HaveListing.City.LocationID", searchLocation.LocationID))
.List<DateBlock>();
给我带来了这样的错误:
could not resolve property: HaveListing.City.LocationID of: Dm.Mvc.Data.UserObjects.DateBlock_US
我一生都无法看出差异可能在哪里。有人能看到有东西向他们跳来吗?或者,有没有什么方法可以从 Criteria(或类似的)获取底层 HQL,以便我可以比较它们?
如果有任何帮助,我的奇怪的-o 映射如下:
public class Map<T> : NotNullableClassMap<T> where T : DateBlock
{
public void BaseMap()
{
Id(b => b.m_BlockCode).Column("BlockCode").GeneratedBy.GuidComb();
Map(b => b.StartDate);
Map(b => b.EndDate);
Map(b => b.BlockType);
}
}
public class DateBlock_US : DateBlock {
public class DateBlock_US_Map : Map<DateBlock_US>
{
public DateBlock_US_Map()
{
base.BaseMap();
References<HaveListing_US>(b => b.HaveListing).Column("HaveListing_US_id");
}
}
}
就像我说的,除了这个之外,它在各个方面都工作得很好。
My (Fluent) NHibernate schema is a little complicated. The two classes in question here are both country-specific, and inherit from an abstract class. The point behind this is that different country data is stored in different tables. It all works fine, except in the following. This HQL executes fine:
dbSession.CreateQuery(@"from DateBlock_US db where
db.HaveListing.City.LocationID = " + searchLocation.LocationID.ToString()).List<DateBlock>();
(I know I should be using SetParameter, but this is just a demo). However, this Criteria doesn't work:
dbSession.CreateCriteria(typeof(DateBlock_US))
.Add(Restrictions.Eq("HaveListing.City.LocationID", searchLocation.LocationID))
.List<DateBlock>();
Giving me the error of:
could not resolve property: HaveListing.City.LocationID of: Dm.Mvc.Data.UserObjects.DateBlock_US
I can't, for the life of me, see where the differences could lie. Can anyone see something jumping out at them? Or, is there any way to get underlying HQL from a Criteria (or similar) so that I might compare them?
In case it's any help, my weird-o mapping is as follows:
public class Map<T> : NotNullableClassMap<T> where T : DateBlock
{
public void BaseMap()
{
Id(b => b.m_BlockCode).Column("BlockCode").GeneratedBy.GuidComb();
Map(b => b.StartDate);
Map(b => b.EndDate);
Map(b => b.BlockType);
}
}
public class DateBlock_US : DateBlock {
public class DateBlock_US_Map : Map<DateBlock_US>
{
public DateBlock_US_Map()
{
base.BaseMap();
References<HaveListing_US>(b => b.HaveListing).Column("HaveListing_US_id");
}
}
}
Like I said, it works fine in every way except this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来解决方案是添加一个 CreateAlias,如下所示:
但是,如果有人能够阐明为什么这是必要的,我将非常感激。
It appears that a solution is to add a CreateAlias, like so:
However, I'd be very grateful if anyone could shed any light on why it is necessary.