如何使用Spring数据JPA连接两个表

如何使用Spring数据JPA连接两个表,spring,spring-data,spring-data-jpa,jpql,Spring,Spring Data,Spring Data Jpa,Jpql,我有两个类似的实体: 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

我有两个类似的实体:

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;
我想加入这些,使用JPA规范,通过DoctorRepository,根据SpecialityId检索所有医生


有人能帮我吗?

在你的
医生档案中
试试这个

@Query("Select d from Doctor d where d.speciality.id = :id")
List<Doctor> findAllBySpeciality(@Param("id") long id);
@Query(“从医生d中选择d,其中d.speciality.id=:id”)
列出findAllBySpeciality(@Param(“id”)长id);
更新1:使用您提供的映射,以下JPQL正在工作

@Query("Select ds.doctor from DoctorSpeciality ds where ds.speciality.id = :id")
List<Doctor> findAllBySpeciality(@Param("id") long id);
@Query(“从DoctorSpeciality ds中选择ds.doctor,其中ds.speciality.id=:id”)
列出findAllBySpeciality(@Param(“id”)长id);

我已成功运行此查询:选择*从医生加入医生专业。id=doctor\u speciality.doctor\u id,其中doctor\u speciality.speciality\u id=2,但我希望使用JPA规范实现相同的结果。@Ganesan检查更新的答案。这个JPQL正在我的机器上成功运行。我上面提到的查询运行良好。但是我想通过这样的规范来实现它:
publicstaticspecification findbysspeciality(Speciality-Speciality){return(root,query,cb)->{joinspecialitydoctor=root.Join(“医生”);joinspecialityrelation=specialityDoctor.Join(“Speciality”);return cb.equal(specialityRelation.get(“Speciality”);};}
它不起作用,我需要帮助。FWIW没有“JPA规范”或“JPA存储库”或“JPA查询”之类的东西。这就是Spring数据JPA(!=JPAAPI)。固定标签