javaspring-data-jpajpa-2.1

JPA: Inheritance.TABLE_PER_CLASS set parent table name


I use InheritanceType.TABLE_PER_CLASS. Somehow I can define the name of the child table, but not of the parent table.

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name="foo")
public class Parent {
    ...
}

@Entity
@Table(name="bar")
public class Child extends Parent {
    ...
}

Results in:

 Schema |  Name  |  Typ   
--------+----------------
 public | parent | Table  
 public | bar    | Table 

What am I doing wrong?


Solution

  • As usual, the problem was in front of the keyboard... me.

    Solution for anyone having the same issue and don't want to waste as much time:

    In the strategy InheritanceType.TABLE_PER_CLASS there is NO parent table. All attributes of the parent class are directly stored in the child tables. So, if there is no parent table, it can not be renamed!

    But why was it listed in my database? Before choosing InheritanceType.TABLE_PER_CLASS I tried differed strategies like InheritanceType.JOINED which in deed creates a parent table. Using the property spring.jpa.hibernate.ddl-auto=create for development, I assumed the database will always match my java implementation (https://stackoverflow.com/a/1689769/8345434). However, create only drops data and not tables which are no longer part of the implementation. Thus, after each application restart I so the old table which was no langer part of the application.

     Schema |  Name  |  Typ  |  Creation Date  
    --------+--------+-------+-----------------
     public | parent | Table | a long time ago 
     public | bar    | Table | now