【发布时间】:2021-11-16 08:05:10
【问题描述】:
我有一个实体“Car”,其中“CarType”作为@ManyToOne @joinColumn 参数:
汽车实体:
@Entity
public class Car implements serializable {
@Id
private Long id;
@ManyToOne(targetEntity = CarType.class)
private CarType carType;
...
}
CarType 实体:
@Entity
public class CarType implements serializable {
@Id
private Long id;
private String name;
private String version;
...
}
CarType 存储库:
@Repository
public interface CarType extends JpaRepository<CarType, Long> {
List<CarType> findAllOrderByNameAsc();
}
用户要求 CarTypes,我已经有一个返回所有 carTypes 的 api,但是其中一些不再使用(没有现有的汽车使用 carType)
有什么方法可以使用 JpaRepository 仅检索“实际”使用的 CarTypes 吗?
【问题讨论】:
-
如何在代码中定义不再使用的 CarTypes?
-
@dope 没有现有汽车使用 carType
-
您可以从数据库中的所有汽车中检索 distinct carType。
-
@ZakFST 你找到解决方案了吗?
标签: java sql spring-data-jpa spring-data