本文摘自一本名为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 表的外键引用?
最佳答案
假设您有三个不同的数据库表PERSON
、STUDENT
和TEACHER
,您将如何定义表ADDRESS
上的外键约束?
CONSTRAINT FOREIGN KEY fk_address_owner
ADDRESS.owner_id
REFERENCES (which table?) (id)
正如您所看到的,您必须指向三个数据库表。这是不可能的。
关于java - 有人可以帮我理解以下与hibernate中的继承策略相关的逻辑吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626316/