在Spring Data JPA中加入两个表实体 [英] Joining two table entities in Spring Data JPA
问题描述
我想写一个类似于 Select *的查询,在a.id = b.id
,
的左边加入b b我是Spring的新手数据JPA。
我不知道如何为Join查询编写实体。
@Entity
@Table name =Release_date_type)
public class ReleaseDateType {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer release_date_type_id;
@Column
私人整数sort_order;
@Column
私有字符串描述;
@Column
private String data_source_type;
@Column(nullable = true)
私人整数media_Id;
@Column
private String source_system;
@Column
private String update_Name;
@Column
私人日期update_Date;
@Column
private String create_Name;
@Column
私人日期create_Date;
@Column
private Integer version_Id;
@Column(nullable = true)
private整数auto_Firm_Flag;
@Column(nullable = true)
private Integer auto_Firm_Months;
@Column(nullable = true)
private整数auto_Firm_Days;
with getters and setters ...
}
另一个实体是
@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)
私人整数位置;
@Column(name =DESCRIPTION)
private String media_Description;
@Column(name =LOAD_DATE)
私人日期loadDate;与getter和setter ..}
我想写一个crudRepository接口。
例如
public interface ReleaseDateTypeRepository扩展CrudRepository< ReleaseDateType,Long> {
@Query(Select *来自A a.b = a.id = b.id的左连接B b)b $ b public List< ReleaseDateType> FindAllWithDescriptionQuery();
}
要执行一对一
关系,您应该更改ReleaseDateType模型中的下一个代码:
@Column(nullable = true)
private Integer media_Id;
用于:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name =CACHE_MEDIA_ID,nullable = true)
私有CacheMedia cacheMedia;
在CacheMedia模型中,您需要添加:
@OneToOne(cascade = ALL,mappedBy =ReleaseDateType)
private ReleaseDateType releaseDateType;
然后在您的存储库中您应该替换:
<
public List< ReleaseDateType> FindAllWithDescriptionQuery();
通过:
//在这种情况下,不需要查询注释,因为spring会从方法名称
构造查询public List< ReleaseDateType> findByCacheMedia_Id(Integer id);
或者:
@Query(FROM ReleaseDateType AS rdt WHERE cm.rdt.cacheMedia.id =?1)//这是使用命名查询方法
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;
然后在您的存储库中您应该替换:
<
public List< ReleaseDateType> FindAllWithDescriptionQuery();
通过:
//在这种情况下,不需要查询注释,因为spring会从方法名称
构造查询public List< ReleaseDateType> findByCacheMedias_Id(Integer id);
或者:
@Query(FROM ReleaseDateType AS rdt LEFT JOIN rdt.cacheMedias AS cm WHERE cm.id =?1)//这是使用命名查询方法
public List< ReleaseDateType> FindAllWithDescriptionQuery(Integer id);
I want to write a query like Select * from A a left join B b on a.id=b.id
,
I am new to Spring Data JPA.
I don't know how to write entities for Join query.
@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...
}
Another entity is 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;
@Column(name="LOAD_DATE")
private Date loadDate; with the getter and setter ..}
I want to write a crudRepository interface. such as
public interface ReleaseDateTypeRepository extends CrudRepository<ReleaseDateType, Long>{
@Query("Select * from A a left join B b on a.id=b.id")
public List<ReleaseDateType> FindAllWithDescriptionQuery();
}
If you want to do a one-to-one
relationship, you should change the next code in ReleaseDateType model:
@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:
@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 @OneToMany
and @ManyToOne
relation, you should change the next code in ReleaseDateType model:
@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:
@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);
这篇关于在Spring Data JPA中加入两个表实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!