Unirse a dos entidades de tabla en Spring Data JPA

Deseo escribir una consulta como Select * from A a left join B b on a.id=b.id , soy nuevo en Spring Data JPA. No sé cómo escribir entidades para la consulta de unión.

 @Entity @Table(name = "Release_date_type") public class ReleaseDateType { @Id @GeneratedValue(strategy=GenerationType.TABLE) private Integer release_date_type_id; @Column private Integer sort_order; @Column private String description; @Column private String data_source_type; @Column(nullable = true) private Integer media_Id; @Column private String source_system; @Column private String update_Name; @Column private Date update_Date; @Column private String create_Name; @Column private Date create_Date; @Column private Integer version_Id; @Column(nullable = true) private Integer auto_Firm_Flag; @Column(nullable = true) private Integer auto_Firm_Months; @Column(nullable = true) private Integer auto_Firm_Days; with getters and setters... } 

Otra entidad es como

 @Entity @Table(name = "Cache_Media") public class CacheMedia{ @Id @GeneratedValue(strategy=GenerationType.TABLE) private Integer id; @Column(name="code") private String code; @Column(name="POSITION") private Integer position; @Column(name="DESCRIPTION") private String media_Description; @Column(name="LOAD_DATE") private Date loadDate; with the getter and setter ..} 

Quiero escribir una interfaz crudRepository. como

 public interface ReleaseDateTypeRepository extends CrudRepository{ @Query("Select * from A a left join B b on a.id=b.id") public List FindAllWithDescriptionQuery(); } 

Si desea establecer una relación one-to-one , debe cambiar el siguiente código en el modelo ReleaseDateType:

 @Column(nullable = true) private Integer media_Id; 

para:

 @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name="CACHE_MEDIA_ID", nullable=true) private CacheMedia cacheMedia ; 

y en el modelo de CacheMedia necesitas agregar:

 @OneToOne(cascade=ALL, mappedBy="ReleaseDateType") private ReleaseDateType releaseDateType; 

entonces en tu repository debes reemplazar:

 @Query("Select * from A a left join B b on a.id=b.id") public List FindAllWithDescriptionQuery(); 

por:

 //In this case a query annotation is not need since spring constructs the query from the method name public List findByCacheMedia_Id(Integer id); 

o por:

 @Query("FROM ReleaseDateType AS rdt WHERE cm.rdt.cacheMedia.id = ?1") //This is using a named query method public List FindAllWithDescriptionQuery(Integer id); 

O si prefiere hacer una relación @OneToMany y @ManyToOne , debe cambiar el siguiente código en el modelo ReleaseDateType:

 @Column(nullable = true) private Integer media_Id; 

para:

 @OneToMany(cascade=ALL, mappedBy="ReleaseDateType") private List cacheMedias ; 

y en el modelo de CacheMedia necesitas agregar:

 @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="RELEASE_DATE_TYPE_ID", nullable=true) private ReleaseDateType releaseDateType; 

entonces en tu repository debes reemplazar:

 @Query("Select * from A a left join B b on a.id=b.id") public List FindAllWithDescriptionQuery(); 

por:

 //In this case a query annotation is not need since spring constructs the query from the method name public List findByCacheMedias_Id(Integer id); 

o por:

 @Query("FROM ReleaseDateType AS rdt LEFT JOIN rdt.cacheMedias AS cm WHERE cm.id = ?1") //This is using a named query method public List FindAllWithDescriptionQuery(Integer id);