NHibernate 使用 Parent Id 查询子项
所以我有一个类似于这个问题的设置: 父子设置
保存父级和子级时,一切都很好。
不过,我在选择孩子的时候似乎遇到了问题。我似乎无法让所有孩子都有一个特定的父母。
失败并显示:NHibernate.QueryException:无法解析属性:ParentEntity_id of:Test.Data.ChildEntity
这是我的代码:
public IEnumerable<ChildEntity> GetByParent(ParentEntity parent)
{
using (ISession session = OrmHelper.OpenSession())
{
return session.CreateCriteria<ChildEntity>().Add(Restrictions.Eq("ParentEntity_id ", parent.Id)).List<ChildEntity>();
}
}
任何帮助构建正确的函数来获取所有项目的帮助都将不胜感激。
哦,我正在使用 Fluent NHibernate 构建映射 - 版本 1 RTM 和 NHibernate 2.1.2 GA
如果您需要更多信息,请告诉我。
根据您的要求,我的流畅映射:
public ParentEntityMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Code).UniqueKey("ukCode");
HasMany(x => x.ChildEntity).LazyLoad()
.Inverse().Cascade.SaveUpdate();
}
public ChildEntityMap()
{
Id(x => x.Id);
Map(x => x.Amount);
Map(x => x.LogTime);
References(x => x.ParentEntity);
}
映射到以下 2 个表:
CREATE TABLE "ParentEntity" (
Id integer,
Name TEXT,
Code TEXT,
primary key (Id),
unique (Code)
)
CREATE TABLE "ChildEntity" (
Id integer,
Amount NUMERIC,
LogTime DATETIME,
ParentEntity_id INTEGER,
primary key (Id)
)
SQLite 中的数据存储。
So I have a set up similar to this questions:
Parent Child Setup
Everything works great when saving the parent and the children.
However, I seem to have a problem when selecting the children. I can't seem to get all the children with a specific parent.
This fails with: NHibernate.QueryException: could not resolve property: ParentEntity_id of: Test.Data.ChildEntity
Here is my code:
public IEnumerable<ChildEntity> GetByParent(ParentEntity parent)
{
using (ISession session = OrmHelper.OpenSession())
{
return session.CreateCriteria<ChildEntity>().Add(Restrictions.Eq("ParentEntity_id ", parent.Id)).List<ChildEntity>();
}
}
Any help in building a proper function to get all the items would be appreciated.
Oh, I am using Fluent NHibernate to construct the mappings - version 1 RTM and NHibernate 2.1.2 GA
If you need more information, let me know.
As per you request, my fluent mappings:
public ParentEntityMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Code).UniqueKey("ukCode");
HasMany(x => x.ChildEntity).LazyLoad()
.Inverse().Cascade.SaveUpdate();
}
public ChildEntityMap()
{
Id(x => x.Id);
Map(x => x.Amount);
Map(x => x.LogTime);
References(x => x.ParentEntity);
}
That maps to the following 2 tables:
CREATE TABLE "ParentEntity" (
Id integer,
Name TEXT,
Code TEXT,
primary key (Id),
unique (Code)
)
CREATE TABLE "ChildEntity" (
Id integer,
Amount NUMERIC,
LogTime DATETIME,
ParentEntity_id INTEGER,
primary key (Id)
)
The data store in SQLite.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用父级本身即可。
Just use the parent itself.
在您的标准中,您不应引用列名称,而应引用属性名称。将“ParentEntity_id”更改为“ParentEntity.Id”,这应该可以解决问题。
In your criteria you should never refer to the column name but to the property name. Change "ParentEntity_id" to "ParentEntity.Id " and that should solve it.