在Spring Data JPA中连接两个表实体

时间:2013-11-14 12:03:03

标签: spring hibernate jpa spring-data-jpa

我想写一个像SELECT * FROM Release_date_type a LEFT JOIN cache_media b on a.id=b.id这样的查询。我是Spring Data JPA的新手。我不知道如何为Join查询编写实体。这是一次尝试:

@Entity
@Table(name = "Release_date_type")
public class ReleaseDateType {

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    private Integer release_date_type_id;
    // ...
    @Column(nullable = true) 
    private Integer media_Id;
    // with getters and setters...
}

另一个实体是:

@Entity
@Table(name = "Cache_Media")
public class CacheMedia {

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    private Integer id;
    // ...
    private Date loadDate; //with the getter and setter ..
}

我想写一个crudRepository界面,例如

public interface ReleaseDateTypeRepository extends CrudRepository<ReleaseDateType, Long>{
    @Query("SELECT * FROM Release_date_type a LEFT JOIN cache_media b on a.id=b.id")
    public List<ReleaseDateType> FindAllWithDescriptionQuery();
}

3 个答案:

答案 0 :(得分:64)

有关拥有一部或多部手机的员工的典型示例,请参阅this wikibook section

对于您的具体示例,如果您想要建立one-to-one关系,则应更改ReleaseDateType模型中的下一个代码:

@Column(nullable = true) 
private Integer media_Id;

有:

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

在CacheMedia模型中,您需要添加:

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

然后在您的存储库中,您应该替换:

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

由:

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

或通过:

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

或者,如果您希望建立@OneToMany@ManyToOne关系,则应更改ReleaseDateType模型中的下一个代码:

@Column(nullable = true) 
private Integer media_Id;

有:

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

在CacheMedia模型中,您需要添加:

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

然后在您的存储库中,您应该替换:

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

由:

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

或通过:

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

答案 1 :(得分:0)

@Query("SELECT rd FROM ReleaseDateType rd, CacheMedia cm WHERE ...")

答案 2 :(得分:0)

这是一个古老的问题,但是解决方案非常简单。如果您不确定如何在休眠状态下编写条件,联接等,则最好的方法是使用本机查询。这不会降低性能,并且非常有用。等式下面

    @Query(nativeQuery = true, value = "your sql query")
returnTypeOfMethod methodName(arg1, arg2);