Hibernate 中每个子类和每个层次结构的混合表
在我的数据库中有两个三表。第一个表 ABSTRACT 包含三列。
id, type, someText
该表包含抽象类 abstract 的所有抽象信息。现在,CONCRETEONE 和 CONCRETETWO 两个表包含具体类 concreteOne 和 concreteTwo 的所有信息。现在我知道我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.