Spring JPA规范,用于过滤与子实体的一对多关系 [英] Spring JPA Specification to filter one to many relation with child entity

查看:303
本文介绍了Spring JPA规范,用于过滤与子实体的一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的实体InwardInventory

I have an entity InwardInventory as below

@Entity
@Table(name = "inward_inventory")
public class InwardInventory extends ReusableFields
{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long in_inventoryId;

    @ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinTable(name = "inventory_entry", joinColumns = {
            @JoinColumn(name = "in_inventoryId", referencedColumnName = "in_inventoryId") }, inverseJoinColumns = {
                    @JoinColumn(name = "entryId", referencedColumnName = "entryId") })
    Set<InwardOutwardList> inwardOutwardList = new HashSet<>();;

//many other fields
}

实体InwardOutwardList具有诸如productId和数量之类的字段.

Entity InwardOutwardList have fields like productId and quantity.

@Entity
@Table(name = "inward_outward_entries")
@Audited
@Where(clause = ReusableFields.SOFT_DELETED_CLAUSE)
public class InwardOutwardList extends ReusableFields
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long entryid;

    @ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name="productId",nullable=false)
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    Product product;
    Float quantity;
//other fields and getter setters
}

我想写一个规范来过滤基于库存的产品ID.示例-如果我将productId传递为100,它将返回所有包含产品100条目的内向库存清单.有人可以帮助我如何在需要查询列表或实体集的地方编写规范.

I want to write a specification to filter inwardinventory based product id. Example - If I pass productId as 100, it should return all inwardinventory list which has entry for product 100. Can someone help me how to write specification where we have to query list or set of entities.

推荐答案

我能够使用联接实现此目的.下面是代码

I was able to achieve this using joins. Below is the code

库存规格代码


public static Specification<InwardInventory> getSpecification(FilterDataList filterDataList) throws ParseException
    {
        List<String> productNames = SpecificationsBuilder.fetchValueFromFilterList(filterDataList,"productNames");

        Specification<InwardInventory> finalSpec = null;

        if(productNames != null && productNames.size()>0)
            finalSpec = specbldr.specAndCondition(finalSpec,specbldr.whereChildFieldListContains(
                    InwardInventory_.INWARD_OUTWARD_LIST,InwardOutwardList_.PRODUCT,Product_.PRODUCT_NAME,productNames));
return finalSpec;
    }

及以下是实现通用方法的代码,该方法可用于具有类似过滤器要求的任何实体类

and below is the code for implementation of generic method which can be used for any entity class having similar filter requirement


public Specification<T> whereChildFieldListContains(String childTableName, String gcTable,String fieldName, List<String> names) 
    {
        Specification<T> finalSpec = null;
        for(String name:names)
        {
            Specification<T> finalSpec = (Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb)
                -> cb.like(root.join(childTableName).join(gcTable).get(fieldName), "%"+name+"%" );

        }
        return finalSpec;

这篇关于Spring JPA规范,用于过滤与子实体的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆