java - 有人可以帮我理解以下与hibernate中的继承策略相关的逻辑吗?

标签 java database hibernate inheritance

本文摘自一本名为Spring persistence with hibernate的书,作者描述了hibernate中每个具体类一个表继承策略的缺点。在给定的示例中,Student 和 Teacher 是 Person 类的子类,Person 与 Address 类关联。我无法理解下面给出的这个特定论点:-

Imperfect support for polymorphic associations: The problem occurs when a parent class is associated with another persistent class. In our example, suppose Person is associated with an Address class, so both Student and Teacher are associated with Address, as well. To map this class hierarchy according to this approach, we need these four tables in our database schema: PERSON, STUDENT, TEACHER, and ADDRESS. If Address has a many-to-one relationship with Person (more than one person may have the same address), then the ADDRESS table should provide a foreign key reference to all PERSON, STUDENT, and TEACHER tables, in order to establish database-level relationship, but this is not possible.

为什么 Address 表无法提供对 PERSON、STUDENT 和 TEACHER 表的外键引用?

最佳答案

假设您有三个不同的数据库表PERSONSTUDENTTEACHER,您将如何定义表ADDRESS上的外键约束?

CONSTRAINT FOREIGN KEY fk_address_owner
ADDRESS.owner_id
REFERENCES (which table?) (id)

正如您所看到的,您必须指向三个数据库表。这是不可能的。

关于java - 有人可以帮我理解以下与hibernate中的继承策略相关的逻辑吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626316/

相关文章:

java - 单击 recyclerAdapter 在大型数据集上工作很奇怪,但在小型数据集上工作正常

具有一个多字段键的 MySQL 索引 : Having multiple indexes vs.?

MySQL - 显示记录及其父项

java - Hibernate 多表批量操作总是尝试创建临时表

java - 从数据库中检索到的实体与查询中的大小写相同

Java - 在类上强制实例化

java - 使用itext pdf缺少pdf上的彩色区域

Java 内存解释 (SUN JVM)

java - 为什么mysql表中的所有记录都被删除了?

java - hibernate 同步更新