Entity view inheritance allows you to materialize different subtypes based on a selection predicate.
Basic Setup
- Annotate the base view with
@EntityViewInheritance. - Annotate subtypes with
@EntityViewInheritanceMapping("predicate").
Restricting Subtypes
You can explicitly list allowed subtypes in the @EntityViewInheritance annotation on the supertype: @EntityViewInheritance({ Subtype.class }).
Inheritance at the Use Site
Use @MappingInheritance on a subview attribute to override or delimit which subtypes are considered for that specific relationship. Use @MappingInheritanceSubtype to define the specific mappings and subtypes. Setting onlySubtypes = true prevents the base type from being materialized (it returns null instead).
JPA Inheritance
If your JPA entities already use inheritance, Blaze-Persistence automatically handles it. If a subtype uses an entity subtype in its @EntityView annotation, Blaze-Persistence generates TYPE(this) = Subtype constraints automatically.
@EntityView(Cat.class)
@EntityViewInheritance
public interface BaseCatView {
String getName();
}
@EntityView(Cat.class)
@EntityViewInheritanceMapping("age < 18")
public interface YoungCatView extends BaseCatView {
@Mapping("mother.name")
String getMotherName();
}
@EntityView(Cat.class)
@EntityViewInheritanceMapping("age > 18")
public interface OldCatView extends BaseCatView {
@Mapping("kittens.name")
List<String> getKittenNames();
}