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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19977130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:22:11  来源:igfitidea点击:

Joining two table entities in Spring Data JPA

springhibernatejpaspring-data-jpa

提问by S Atah Ahmed Khan

I want to write a query like SELECT * FROM Release_date_type a LEFT JOIN cache_media b on a.id=b.id. I am new to Spring Data JPA. I don't know how to write entities for Join query. Here is an attempt:

我想写一个像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...
}

Another entity is:

另一个实体是:

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

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

I want to write a crudRepositoryinterface such as

我想写一个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();
}

回答by Luis Vargas

For a typical example of employees owning one or more phones, see this wikibook section.

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

For your specific example, if you want to do a one-to-onerelationship, you should change the next code in ReleaseDateType model:

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

@Column(nullable = true) 
private Integer media_Id;

for:

为了:

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

and in CacheMedia model you need to add:

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

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

then in your repository you should replace:

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

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

by:

经过:

//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); 

or by:

或通过:

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

Or if you prefer to do a @OneToManyand @ManyToOnerelation, you should change the next code in ReleaseDateType model:

或者,如果您更喜欢使用@OneToManyand@ManyToOne关系,您应该更改 ReleaseDateType 模型中的下一个代码:

@Column(nullable = true) 
private Integer media_Id;

for:

为了:

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

and in CacheMedia model you need to add:

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

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

then in your repository you should replace:

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

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

by:

经过:

//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); 

or by:

或通过:

@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);

回答by Yrineu Rodrigues

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

回答by Kunal Vohra

This has been an old question but solution is very simple to that. If you are ever unsure about how to write criterias, joins etc in hibernate then best way is using native queries. This doesn't slow the performance and very useful. Eq. below

这是一个老问题,但解决方案非常简单。如果您不确定如何在 hibernate 中编写条件、连接等,那么最好的方法是使用本机查询。这不会降低性能并且非常有用。等式 以下

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