@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Foo
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class BarFoo extends Foo
mysql> desc foo;
+---------------+-------------+
| Field | Type |
+---------------+-------------+
| id | int |
+---------------+-------------+
mysql> desc barfoo;
+---------------+-------------+
| Field | Type |
+---------------+-------------+
| id | int |
| foo_id | int |
| bar_id | int |
+---------------+-------------+
mysql> desc bar;
+---------------+-------------+
| Field | Type |
+---------------+-------------+
| id | int |
+---------------+-------------+
是否可以指定列 barfo.foo_id 作为连接列?
由于您要重写 Foo 类的 getter/seeter,因此是否可以将 barfoo.id 指定为 BarFoo 的 @Id?
我了解这种关系背后的原理图(或者至少我认为我了解)并且我对此很满意。
我想要 BarFoo 的显式 id 字段的原因正是因为我想在查询 BarFoo 或在“更强”约束中使用时避免使用连接键(foo _id,bar _id)。 (正如鲁本所说)
最佳答案
定义 InheritanceType.JOINED 时,@Id 列将自动用于连接。 Barfoo 实例只有一个 id 值,并且该值保留在 foo 和 barfoo 表的 id 列中。
barfoo 表中不需要有单独的 id 和 foo_id 列。默认情况下也没有必要覆盖 barfoo 中的 id getter 和 setter。仅当 Barfoo 实例的 id 比 Foo 实例的 id 具有更强的约束时,您才会覆盖 id setter 和 getter。
关于java - 如何为 @Inheritance JPA 注释指定不同的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1227012/