@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指定为联接的列?
是否允许将barfoo.id指定为BarFoo的@Id,因为您正在覆盖Foo类的getter/seeter?
我理解这种关系背后的原理(或者至少我认为我理解),我对它们没有意见。
我想要一个显式的BarFoo id字段的原因正是因为我想避免在查询BarFoo或在“更强”的约束中使用时使用连接键(foo _id,bar _id)。(正如鲁本所说)
发布于 2009-08-04 12:00:14
当定义了InheritanceType.JOINED时,@Id列将自动用于连接。一个Barfoo实例只有一个id值,这个值保存在foo表和barfoo表的id列中。
不需要在barfoo表中有单独的id和foo_id列。默认情况下,也没有必要覆盖barfoo中的id、getter和setter。只有当Barfoo实例的id比Foo实例的id有更强的约束时,才会覆盖id、setter和getter。
发布于 2010-06-18 22:22:07
查看@PrimaryKeyJoinColumn
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name="CHILD_PK1",
referencedColumnName="PARENT_PK1"),
@PrimaryKeyJoinColumn(name="CHILD_PK2",
referencedColumnName="PARENT_PK2")
})
public class Child extends Parent {
...
}https://stackoverflow.com/questions/1227012
复制相似问题