0

I have this strange hierarchy:

  • Class A is abstract (but it is an entity)
  • Class B (abstract) and C both extend class A (TABLE_PER_CLASS).
  • Class B is also extended by classes D and E but this time it is JOINED.

I want the ID of the A class to be the ID of all others.

Can this be done?

I get strange errors of IDs. can any one show me how to map?

This is how I did it:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class A {
      @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
    protected Integer id;
}

Class C:

@Entity
@Table(name = "managers")
public class C extends A {

Class B:

@Entity
@Table(name = "bb")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class B extends A {

Class C,D:

@Entity
@Table(name = "cc"/"dd")
@DiscriminatorValue("CC"/"DD")
public class C (or D) extends B {
Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
Ido Barash
  • 4,856
  • 11
  • 41
  • 78
  • Have you looked at [Mixing Joined and Table per concrete class inheritance](http://stackoverflow.com/questions/5977717/mixing-joined-and-table-per-concrete-class-inheritance)? – Greg Kopff Mar 31 '14 at 11:41

1 Answers1

1

While mixing inheritance strategies in an inheritance tree can be sort of accomplished (https://stackoverflow.com/a/3916998/131929 refers to Java Persistence with Hibernate -> 5.1.5 Mixing inheritance strategies (p207-p210)) I, and everybody else I know, advise against it.

Sidenote, I suggest you always use @MappedSuperclass rather than @Entity for abstract classes.

Community
  • 1
  • 1
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198