我正在对两种类型的变量进行建模,分别称为AVar
和BVar
。它们每个都可以与多个类型的值相关联AValue
,并且BValue
。我决定使用继承,这样每个具体类都继承自一个抽象基类(变量或值)。
然后我有一个从抽象值到抽象变量的多对一映射。对于变量,我使用SINGLE_TABLE
继承。对于价值,我正在使用TABLE_PER_CLASS
继承。Hibernate 支持所有这些并且可以很好地生成表,但是,我得到相同的外键约束 3 次,这是双重冗余的。我的设置有问题吗,或者这是 Hibernate 错误?
Hibernate 模式更新的输出:
HHH000262: Table not found: dm_variables
HHH000262: Table not found: dm_variables_a_values
HHH000262: Table not found: dm_variables_b_values
create table dm_variables (type varchar(31) not null, id varchar(36) not null, version bigint not null, name varchar(255) not null, variable_set_id varchar(36) not null, primary key (id), unique (variable_set_id, name))
create table dm_variables_a_values (id varchar(36) not null, version bigint not null, [current] bit not null, variable_id varchar(36) not null, member_id int, primary key (id))
create table dm_variables_b_values (id varchar(36) not null, version bigint not null, [current] bit not null, variable_id varchar(36) not null, string_value varchar(255), primary key (id))
alter table dm_variables_a_values add constraint FK__dm_values__variable_id90d2dd34 foreign key (variable_id) references dm_variables
alter table dm_variables_a_values add constraint FK7ED02C351F5CFA0E90d2dd34 foreign key (variable_id) references dm_variables
alter table dm_variables_a_values add constraint FK7ED02C35F9E9BFC090d2dd34 foreign key (variable_id) references dm_variables
alter table dm_variables_b_values add constraint FK__dm_values__variable_id5135dfd4 foreign key (variable_id) references dm_variables
alter table dm_variables_b_values add constraint FK7ED02C351F5CFA0E5135dfd4 foreign key (variable_id) references dm_variables
alter table dm_variables_b_values add constraint FK7ED02C35F9E9BFC05135dfd4 foreign key (variable_id) references dm_variables
HHH000232: Schema update complete
代码(简化):
@Entity
@Table(name = "dm_variables")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Variable extends BaseEntity {
}
@Entity
@DiscriminatorValue(value = "A")
public class AVar extends Variable {
}
@Entity
@DiscriminatorValue(value = "B")
public class BVar extends Variable {
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Value extends BaseEntity {
@ManyToOne
@JoinColumn(name = "variable_id", nullable = false)
@ForeignKey(name = "FK__dm_values__variable_id")
private Variable variable;
}
@Entity
@Table(name = "dm_variables_a_values")
public class ValueA extends Value {
}
@Entity
@Table(name = "dm_variables_b_values")
public class ValueB extends Value {
}