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

发布于 2024-08-16 01:14:19 字数 1191 浏览 2 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2024-08-23 01:14:19

您不能这样做,如果关联表具有您需要的属性,则必须将关联映射为一对多(映射到 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)

萌逼全场 2024-08-23 01:14:19

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

Torkel is correct. You need to not only create the new linking entity, but also change the collection for each of the entities to a collection of your new linking entity. Then change the mappings as appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文