java - 使用 JpaRepository 过滤子类单表的子类实体
问题描述
我有一个名为 Vehicle 的子类,它是抽象的,并由两个子类 Car 和 Motorbike 扩展。
在 DAO 层上,我只为 Vehicle单表创建了一个接口存储库,其中包括所有车辆类型(汽车和摩托车),在这个存储库接口中,我需要创建一个方法 findCars,它只返回主表 Vehicle 中的 Car 实体,并且另一种方法 findMotorbikes 返回 Motorbike 实体,请问我该如何操作。
这是 Vehicle 类的代码:
@Entity
@Table(name = "Vehicle")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "Type", discriminatorType = DiscriminatorType.STRING, length = 10)
public abstract class Vehicle {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String model;
}
车类:
@Entity
@DiscriminatorValue("Car")
public class Car extends Vehicle {
private String x;
}
摩托车类:
@Entity
@DiscriminatorValue("Motorbike")
public class Motorbike extends Vehicle {
private String y;
}
解决方案
推荐阅读
- javascript - 过去 2 小时的 IST 格式 javascript 日期和时间
- python - 在 Python 中从此趋势线图中提取 y 值
- java - 如何测试传递给模拟对象的功能接口 [Mockito,Spring]
- java - java.utill.Time 和 java.sql.time 有什么区别
- ios - APNs 身份验证密钥是否会过期?
- android - 识别被触摸的视图
- ionic-framework - 无法使用 Ionic 3 使用无限滚动
- machine-learning - 如何在生产流程中不断提高tensorflow对象检测模型的准确率?
- javascript - 多个类似的“选择选项”ID,动态 Javascript 代码
- r - R:快速生成部分序列