1

In C# we are using an ORM that lets us specify in each child/Sub Class where we want to store it:

[MapInheritance(MapInheritanceType.ParentTable)]  //this means that store employee specific fields in person table
public partial class Employee: Person  
{}

I want to use the same in Java side,But in Hibernate we specify strategy in parent Class .We have following structure: enter image description here

enter image description here

I don't want a table for base class.I'd like to store person & employee in user table. and customer in its own table.

But it seems that hibernate has shortcoming in this regard and asks for one hierarchy policy for all branch. I want to be able to change policy in lower branches.

How is it possible to implement this in jpa or hibernate?

Behdad
  • 184
  • 3
  • 12
  • 1
    It's not. You can work around this limitation for [joined + single-table strategies](https://stackoverflow.com/questions/3915026/how-to-mix-inheritance-strategies-with-jpa-annotations-and-hibernate) but not for single-table + table-per-class strategies – crizzis Nov 27 '20 at 19:16
  • Yes it is possible. What was your attempt?. I have a Base.java equal to yours and classes like: Option.java, Application.java which extend of Base.java. Result tables are just: Option and Application. Is this what you need? – JRichardsz Nov 28 '20 at 14:27

1 Answers1

-1

You can do that using @MappedSuperclass

One @MappedSuperclass or multiple @MappedSuperclass are allowed in same inheritance hierarchy.

@MappedSuperclass
public class FirstMapped {
    @Id int id;
}

@MappedSuperclass
public class SecondMapped extends FirstMapped {
    String createdOn;
}

@Entity
public class MyTable extends SecondMapped {
    String myColumn;
}

These classes must create just one table: MyTable with columns:

  • id
  • createdOn
  • myColumn
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • I believe that: 'I'd like to store person & employee in user table. and customer in its own table' is a pretty explicit statement about what the OP wants, and is in direct opposition to 'These classes must create just one table' – crizzis Nov 29 '20 at 10:43
  • I was guided by this statement: `I don't want a table for base class`. In another way we both know that question owner sometimes is not clear about what he needs. Finally I will wait his feedback. – JRichardsz Nov 29 '20 at 14:09