Spring data jpa @OneToMany 在一的一端进行查询()对集合属性设置条件查询)
2020-12-13 05:48
标签:top integer lis code 库存 rri 关联 epo tin 业务场景: 一个商品对应多个仓存,需要查询商品在某个或某几个库存中存在时,查询出来. 实体类 ,商品Goods 实体类,仓库 当使用spring data jpa 的@query简单查询时,需要在HQL 中使用 join Repository中的方法 以上是固定查询时的用法,很多时候我们使用了Spring data jpa 的动态查询 即Specification类,此时的业务代码如下 Spring data jpa @OneToMany 在一的一端进行查询()对集合属性设置条件查询) 标签:top integer lis code 库存 rri 关联 epo tin 原文地址:https://www.cnblogs.com/java-arthur/p/11149202.html@Entity
@Table(name = "es_goods")
public class Goods {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "goods_id")
private Integer id;
private String name;
//仓库集合
@OneToMany(mappedBy = "goods", cascade = CascadeType.ALL)
private List
@Entity
@Table(name = "es_product_store")
public class ProductStore {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
//商品
@ManyToOne
@JoinColumn(name="goods_id")
private Goods goods;
//仓库名,理论上应该使用一个关联,此处为了简便就只用的一个String
private String name;
}
// @Query("select g from Goods g where g.productStores.name = ?1 ") 错误写法,启动报错
@Query("select DISTINCT g from Goods g left join g.productStores as p where p.name = ?1")//正确
public List
new Specification
文章标题:Spring data jpa @OneToMany 在一的一端进行查询()对集合属性设置条件查询)
文章链接:http://soscw.com/index.php/essay/31794.html