Hibernate Criteria Query - 如何使用属性搜索多对多?

时间:2009-12-17 03:50:55

标签: nhibernate activerecord criteria castle-activerecord

我正在尝试创建一个Criteria查询来选择通过关联表相关的对象。

Insurer * - 1 Insurer_Section 1 - * Section

InsurerSection具有关联属性:active:bool。

如何将InsurerSection类中的活动属性设置为true的所有保险公司?

PS:我不能这样:

Insurer.FindAll(
DetachedCriteria.For<Insurer>().CreateCriteria("Insurer_Section").Add(Expression.Eq("Active", true)
);

因为Insurer_Section是一个仅通过HasAndBelongsToMany映射的关联表:

[HasAndBelongsToMany(typeof(Section), Table = "`Insurer_Section`", 
            ColumnKey = "`IdInsurer`", ColumnRef = "`IdSection`",
            Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
        private IList<Section> Sections {
            get { return this.sections; }
            set { this.sections = value; }
        }

[HasAndBelongsToMany(typeof(Insurer), Table = "`Insurer_Section`",
            ColumnKey = "`IdSection`", ColumnRef = "`IdInsurer`",
            Cascade = ManyRelationCascadeEnum.None, Inverse = true)]
        public IList<Insurer> Insurers {
            get { return this.insurers; }
            set { this.insurers = value; }
        }

2 个答案:

答案 0 :(得分:2)

您不能这样做,如果您的关联表具有您需要的属性,您必须将关联映射为一对多(对于Insurer_Section的新enity),然后具有多对一关系 与部分的关系。

当关联表不仅仅是主键和可能的索引列时,您需要将关联表映射为链接两个实体的单独实体(保险公司和部分以及关联信息,如IsActive)< / p>

答案 1 :(得分:0)

Torkel是对的。您不仅需要创建新的链接实体,还需要将每个实体的集合更改为新链接实体的集合。然后根据需要更改映射。

相关问题