简体   繁体   中英

Joining two table in spring data JPA and Querying two table data from repository

I am using Sprind JPA, Spring 3.1.2(in future 3.2.3), Hibernate 4.1 final. I am new to Sprind Data JPA. I have tow Table Release_date_type and Cache_media which entities are as follows :

ReleaseAirDate.java

@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; with getters and setters..

and CacheMedia as

@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; with setter and getters.

Now my repository interface is as follows :

public interface ReleaseDateTypeRepository extends CrudRepository<ReleaseDateType, Long>{ }

Now i want to write a method(Query) in ReleaseDateTypeRepository interface which can get all the data from Release_Date_Type table including appropriate media_description from Table 'Cache_Media' based on media_id of Release_date_type table.

So my select (SQL)query looks like

SELECT * from Release_Date_Type r left join Cache_Media c on r.media_id=c.id

I don't know how to map entities. I tried so many thing but not luck. Any help is appreciated.

它不是通过Hibernate进行联接的答案,但是您也可以使用联接创建视图并将该视图映射到对象

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM