0

I have created a view for a table. This view is a copy of the table, except that it filters the elements. Nothing complicated. I am on a big project, the @Entity code of the table is already quite extensive and complex. Knowing that duplicating this code, will imply that the developers working on it, will have to take into account that the two codes must be identical, but with two different @Entity. I wanted to know if there was a way to use polymorphism to avoid having to copy the existing code for the view.

I've already looked at the @Inheritance documentation, but none of them match. The problem is that Hibernate interprets 'extends' on some behaviors. For example:

  • InheritanceType.SINGLE_TABLE, the default behavior, considers both @Entity as being for the same table, but I want to retrieve data from 2 different tables/views.
  • InheritanceType.TABLE_PER_CLASS, retrieves elements from the child table and adds them to the parent table.

I just want to use the same behavior but for a different table.

I also thought about using @MappedSuperclass, but the problem is that it involves moving a significant amount of code. The code of the concerned @Entity is several thousand lines long.

@Entity
@Table(name = "PARENT")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class ParentEntity implements Serializable {
    // ...
}

@Entity
@Table(name = "PARENT_VIEW")
public class ChildEntity extends ParentEntity implements Serializable {
    // ...
}

This question has been asked before, but has never been resolved

Hibernate @Inheritance with table as parent and view as child

sushiLover
  • 56
  • 2
  • 8
  • 1
    `@MappedSuperclass` is definitely a possible and pretty easy solution. Rename `ParentEntity` to something like `AbstractParent`, recreate a new `ParentEntity` that extends this `AbstractParent`. Also have `ChildEntity` extend the `AbstractParent`. No need to move any code around – XtremeBaumer Mar 10 '23 at 10:30

0 Answers0