JPA / Hibernate - InheritanceType.JOINED behaves like InheritanceType.TABLE_PER_CLASS

I am trying to implement a very simple inheritance model in Hibernate. Basically, I have one superclass, which can be called A , and several subclasses, all of which inherit from A Since the behavior that I see is the same for all of them, they can simply be called B

I am trying to find what is described here in section 6.2. Basically, there should be a table for A that contains its fields, and a table for B that contains only fields that are different from the subclass, plus a join column back to the table for A I use Hibernate auto-build circuitry (enabled only for stability unit).

However, when I look at the diagram, this table for A contains its fields (correctly) and the table for B , which contains all the fields in A (incorrect), plus the fields added to B My classes are annotated as follows:

 @Entity @Table(name="A") @Inheritance(strategy = InheritanceType.JOINED) public class A implements Serializable { protected long id; protected Date createDate; protected String title; protected boolean hidden; public A() { } @Id @GeneratedValue(strategy = GenerationType.AUTO) public long getId() { return id; } @Column(nullable = false) @Temporal(TemporalType.TIMESTAMP) public Date getCreateDate() { return createDate; } @Column(nullable = false) public boolean isHidden() { return hidden; } @Column(nullable = false) public String getTitle() { return title; } //also setters... } @Entity @Table(name="B") @PrimaryKeyJoinColumn(name="aId", referencedColumnName="id") public class B extends A { private String extraField; public B() { super(); } @Column public String getExtraField() { return extraField; } //also setter... } 

Any ideas what I did wrong? In particular, what I want to see when I look at the generated database schema looks something like this:

 Table A: {id, createDate, title, hidden} Table B: {aId, extraField} 

... and instead I get:

 Table A: {id, createDate, title, hidden} Table B: {id, createDate, title, hidden, extraField} 

Is this simply not possible using the Hibernate automatic schema scheme, or did I screw annotations somewhere?

+4
source share
1 answer

Your annotation is correct, it should create the table schema that you want.

But now you get an unwanted scheme, which is exactly the scheme created using the Table per concrete class @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) (ie @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) ). So, I think one of the possible reasons is that the hibernate.hbm2ddl.auto property in your configuration uses the default value, which is update .

The behavior of the update value:

  • Hibernate will attempt to create an update script to update the database schema for the current display when a SessionFactory is created.

  • If the update instruction cannot be executed, it will be skipped (for example, adding a column is not a null table with existing data)

  • Hibernate will not delete data during the update (for example, if the column name is changed, it just add a new column with a new name, but still keep the column with the original name)

So, I think you should use @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) to generate the schema before, which created the next schema. Table A and table B do not have foreign key associations with each other.

 Table A: {id, createDate, title, hidden} Table B: {id, createDate, title, hidden, extraField} 

After that, you changed the use of @Inheritance(strategy = InheritanceType.JOINED) . During the upgrade schema process, hibernate simply updated your schema by adding a foreign key association between TableA.id and TableB.id . He saved all the other columns in table B. That's why you get the current layout, even if your annotation is correct.

The desired table schema must be generated after you drop table A and table B from the database before starting the sleep program. Alternatively, you can set hibernate.hbm2ddl.auto to create , then hibernate will delete all tables before creating the table schema.

+4
source

All Articles