我有两个类似于以下内容的实体:
Doctor:
@Id
@Column(name = "ID", nullable = false, updatable = false)
private Long id;
...
DoctorSpeciality:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false, updatable = false)
private Long id;
@ManyToOne
@JoinColumn(name = "DOCTOR_ID")
private Doctor doctor;
@ManyToOne
@JoinColumn(name = "SPECIALITY_ID")
private Speciality speciality;
我想加入这些,通过DoctorRepository使用JPA规范检索所有医生,给予SpecialityId。
有人可以帮我这个吗?
答案 0 :(得分:3)
在DoctorRepository
试试这个
@Query("Select d from Doctor d where d.speciality.id = :id")
List<Doctor> findAllBySpeciality(@Param("id") long id);
更新1:使用您提供的映射,以下JPQL正在运行。
@Query("Select ds.doctor from DoctorSpeciality ds where ds.speciality.id = :id")
List<Doctor> findAllBySpeciality(@Param("id") long id);