Hibernate Criteria Query - 如何使用属性搜索多对多?
我正在尝试创建一个条件查询来选择通过关联表相关的对象。
Insurer * - 1 Insurer_Section 1 - * Section
InsurerSection 有一个关联属性:active:bool。
如何获取 InsurerSection 类中的 active 属性设置为 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; }
}
AND
[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; }
}
I'm trying to create a Criteria query to select objects that are related via an association table.
Insurer * - 1 Insurer_Section 1 - * Section
InsurerSection has an association attribute: active:bool.
How do I get all Insurers whose active attribute in the InsurerSection class is set to true?
PS: I can't go like this:
Insurer.FindAll(
DetachedCriteria.For<Insurer>().CreateCriteria("Insurer_Section").Add(Expression.Eq("Active", true)
);
because Insurer_Section is an association table that is only mapped via 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; }
}
AND
[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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您不能这样做,如果关联表具有您需要的属性,则必须将关联映射为一对多(映射到 Insurer_Section 的新实体),然后该关联具有多对一
与节的关系。
一旦关联表变得不仅仅是主键和可能的索引列,您将需要将关联表映射为链接两个实体的单独实体(保险公司和部分以及关联信息,如 IsActive)
You can't do that, if you association table has properties you need you must map the association as a one-to-many (to an new enity for Insurer_Section) that then has a many-to-one
relation to Section.
The moment the association table becomes more than just a the primary keys and possible index columns you will need to map the association table as a separate entity linking the two entities (Insurer and Section together with the association information, like IsActive)