0

I'm using JPA JOINED inheritance strategy with 3 abstract classes and 2 concrete. Like this (code reduced for example purpose):

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
abstract class Employee {
    @Id
    private Long id;

    private String firstName;
    private String secondName;
}

@Entity
abstract class ProjectManager extends Employee {
    @OneToMany(mappedBy="manager")
    private List<Developer> developers;
}


@Entity
abstract class Developer extends Employee {
    @ManyToOne
    @JoinColumn(name="project_manager_id")
    private ProjectManager manager;
}

@Entity
class JavaProjectManager extends ProjectManager {
    private String someCustomProperty;
}

@Entity
class JavaDeveloper extends Developer {
    private String skill;
}

This way, JPA creates 5 tables. But one of this tables, abstract ProjectManager it is empty. I mean, ProjectManager table only have ID column due to inheritance strategy.

My question is: how I can avoid this extra table for ProjectManager abstract class but keeping the same hierarchy of classes ?

I could not remove @Entity on ProjectManager class because I still need bidirectional relationship with Developer class.

Edit:

Also, I don't want move developers OneToMany from ProjectManager to JavaProjectManager, because I have more classes that extends from ProjectManager and need to have developers.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Danny
  • 682
  • 1
  • 11
  • 21
  • please see https://stackoverflow.com/questions/3827494/inherited-abstract-class-with-jpa-hibernate – ibexit Oct 25 '18 at 20:34

1 Answers1

1

InheritanceType.TABLE_PER_CLASS does exactly what you want.

This type only creates/uses tables for non-abstract tables.

Read more about the various types here:

https://www.thoughts-on-java.org/complete-guide-inheritance-strategies-jpa-hibernate/

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82