Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate is generating the same foreign key constraint multiple times with inheritance strategy SINGLE_TABLE
    primarykey
    data
    text
    <p>I am modelling two types of variables, call them <code>AVar</code>, and <code>BVar</code>. They each can be associated with multiple values, of type <code>AValue</code>, and <code>BValue</code> respectively. I decided to use inheritance, so that each concrete class inherits from an abstract base class (Variable or Value).</p> <p>I then have a ManyToOne mapping from the abstract Value to abstract Variable. For Variable I am using <code>SINGLE_TABLE</code> inheritance. For Value, I am using <code>TABLE_PER_CLASS</code> inheritance. Hibernate supports all this and generates the tables fine, however, I get the same foreign key constraint 3 times, which is doubly redundant. Is there something wrong with my set up, or is this a Hibernate bug?</p> <p>Output from Hibernate schema update:</p> <pre><code>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 </code></pre> <p>Code (simplified):</p> <pre><code>@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 { } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload