简体   繁体   English

NHibernate子项查询,使用父ID

[英]NHibernate Child items query using Parent Id

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 失败的原因是:NHibernate.QueryException:无法解析属性:Test.Data.ChildEntity的ParentEntity_id

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 哦,我正在使用Fluent NHibernate来构建映射-版本1 RTM和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. 数据存储在SQLite中。

return session.CreateCriteria<ChildEntity>()
  .Add(Restrictions.Eq("ParentEntity", parent))
  .List<ChildEntity>();

Just use the parent itself. 只需使用父项本身即可。

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. 将“ ParentEntity_id”更改为“ ParentEntity.Id”,这应该可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM