Hibernate 中每个子类和每个层次结构的混合表

发布于 2024-09-01 14:38:39 字数 333 浏览 3 评论 0原文

在我的数据库中有两个三表。第一个表 ABSTRACT 包含三列。

id, type, someText

该表包含抽象类 abstract 的所有抽象信息。现在,CONCRETEONECONCRETETWO 两个表包含具体类 concreteOneconcreteTwo 的所有信息。现在我知道我可以使用 hibernate 中的每个子类表策略来创建具有继承的映射。但是,由于我有一列标记了具体实现的类型,是否可以创建一些混合行为,例如带有鉴别器的每个子类策略的表?

In my database there are two three tables. The first one, table ABSTRACT, holds three columns

id, type, someText

This table contains all abstract information for the abstract class abstract. Now the two tables CONCRETEONE and CONCRETETWO contain all information for the concrete classes concreteOne and concreteTwo. Now I know I could use the table per subclass strategy from hibernate to create a mapping with inheritance. But as I have a column that marks the type of the concrete implementation could it be possible to create some mixed behaviour like a table per subclass strategy with an discriminator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吹泡泡o 2024-09-08 14:38:39

Hibernate继承映射策略应该根据数据库设计来选择。

如果无法更改数据库设计,则坚持当前数据库设计的最佳策略。即每个子类策略的表。

如果您可以更改数据库设计,请先忘记 Hibernate,专注于哪种数据库表设计更适合您的应用程序。
需要考虑的一些因素,
a) 如何获取/使用数据?
可以说,如果大多数时候,逻辑不关心区分 CONCRETEONE 和 CONCRETETWO,那么就没有必要使用子表。
b) CONCRETEONE 和 CONCRETETWO 需要多少个附加列?
如果您有很多特定于子表的列,那么“每个类层次结构表”不是一个好主意。
c) 特定于子表的列不应为“NOT-NULL”。

希望这会有所帮助。

Hibernate inheritance mapping strategies should be selected based on the database design.

If you can not change the database design, then stick with the best strategy for the current DB design. i.e table per subclass strategy.

If you can change the database design, forget about Hibernate first, focus on which DB table design is better for your application.
Some factors to consider,
a) How the data will be fetched/used?
Lets say, if most of the time, the logic not concern about differentiating CONCRETEONE and CONCRETETWO, then no point of going for sub tables.
b) How many additinal columns are required to CONCRETEONE and CONCRETETWO?
If you have so many columns specific to sub tables then "Table per class herarchy" is not a good idea.
c) Columns specific to sub tables should not be "NOT-NULL".

Hope this will help.

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