首页 > 解决方案 > 使用 Spring Data JPA 映射多级继承的最佳方法是什么?

问题描述

我的项目中有这种类型的结构:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Vehicle implements Serializable {}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ENGINE_TYPE", discriminatorType = DiscriminatorType.STRING)
public class Car extends Vehicle {}
@DiscriminatorValue("E")
@Entity
public class ElectricCar extends Car {}
@DiscriminatorValue("ICE")
@Entity
public class InternalCombustionEngineCar extends Car {}
@DiscriminatorValue("H")
@Entity
public class HybridcCar extends Car {}

Vehicle类有@Inheritance(strategy = InheritanceType.JOINED) 并且我想@Inheritance(strategy = InheritanceType.SINGLE_TABLE)与类一起使用,Car因为每个子类中只有 3 个子类具有 1 个属性。但问题是即使我SINGLE_TABLE为类指定了策略Car,它仍然为每个子类创建单独的表。有什么办法可以修复吗?还是有更优化的解决方案?

谢谢

标签: javaspringhibernateinheritancespring-data-jpa

解决方案


推荐阅读