单向Manytomany JPA规范
我有以下实体 StockItem
,产品
和productCategory
。
stockitem
与产品
- 产品单向带有
productcategory
procem> process> product
class class没有参考的参考productCategory
,但是productCategory
收集了product
我想使用JPS规范来查询给定的ProductCategory
类的概述,如下所示。
库存项目类看起来像是
@Entity
public class StockItem {
...
@ManyToOne
private Product product;
...
}
产品类骨骼
@Entity
public class Product {
...
private String productName;
...
}
产品类别的类别如下:
@Entity
public class ProductCategory {
...
@NotNull
private String categoryName;
@ManyToMany
@NotNull
private Set<Product> products = new HashSet<>();
...
}
我想根据ProductCategoryId查询SalelineItems。我尝试了一个,但我坚持以下代码。
public List<SaleLineItem> findAllByProductCategory(LongFilter productCategoryFilter,
Specification<SaleLineItem> specification) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<SaleLineItem> cq = cb.createQuery(SaleLineItem.class);
Root<ProductCategory> productCategoryRoot = cq.from(ProductCategory.class);
ListJoin<ProductCategory, Product> lj = productCategoryRoot.joinList(ProductCategory_.PRODUCTS);
Root<StockItem> stockItemRoot = cq.from(StockItem.class);
Join<StockItem, Product> kl = stockItemRoot.join(StockItem_.product);
// How to combine lj and kl :)
return null;
}
实现这一目标的方法是什么?
I have the following entitiesStockItem
, Product
and ProductCategory
.
StockItem
ManyToOne withProduct
Product
Unidirectional ManyToMany withProductCategory
Product
class has no reference of ProductCategory
, but ProductCategory
has collection of Product
I wanted to use JPS specification to query StockItems for a given ProductCategory
Class outlines are as below.
STOCK ITEM class looks like
@Entity
public class StockItem {
...
@ManyToOne
private Product product;
...
}
PRODUCT class skeleton
@Entity
public class Product {
...
private String productName;
...
}
PRODUCT CATEGORY is as follows
@Entity
public class ProductCategory {
...
@NotNull
private String categoryName;
@ManyToMany
@NotNull
private Set<Product> products = new HashSet<>();
...
}
I wanted to query SaleLineItems based on productCategoryId. I made an attempt but am stuck with the following code.
public List<SaleLineItem> findAllByProductCategory(LongFilter productCategoryFilter,
Specification<SaleLineItem> specification) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<SaleLineItem> cq = cb.createQuery(SaleLineItem.class);
Root<ProductCategory> productCategoryRoot = cq.from(ProductCategory.class);
ListJoin<ProductCategory, Product> lj = productCategoryRoot.joinList(ProductCategory_.PRODUCTS);
Root<StockItem> stockItemRoot = cq.from(StockItem.class);
Join<StockItem, Product> kl = stockItemRoot.join(StockItem_.product);
// How to combine lj and kl :)
return null;
}
What would be the way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将为您提供链接表所需的谓词,您可以将其作为谓词返回,以便使用方法。
另外,您可以使用它来定义LT上的JOIN语句:
但是如果您返回null,我不知道弹簧如何处理查询。
Will give you the predicate you need to link your tables which you can return as the predicate for your method to use.
Alternatively, you can use it to define the join statement on lt using:
but I don't know how Spring handles the query if you return null.