单向Manytomany JPA规范

发布于 2025-01-20 01:11:24 字数 1853 浏览 1 评论 0原文

我有以下实体 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;
    }

erd图

实现这一目标的方法是什么?

I have the following entities
StockItem, Product and ProductCategory.

  • StockItem ManyToOne with Product
  • Product Unidirectional ManyToMany with ProductCategory

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;
    }

ERD Diagram

What would be the way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2025-01-27 01:11:24
cb.equals(lj.get(Product_.id), kl.get(Product_.id)

将为您提供链接表所需的谓词,您可以将其作为谓词返回,以便使用方法。

另外,您可以使用它来定义LT上的JOIN语句:

lj.on(cb.equals(lj.get(Product_.id), kl.get(Product_.id));

但是如果您返回null,我不知道弹簧如何处理查询。

cb.equals(lj.get(Product_.id), kl.get(Product_.id)

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:

lj.on(cb.equals(lj.get(Product_.id), kl.get(Product_.id));

but I don't know how Spring handles the query if you return null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文