繁体   English   中英

Spring Data Jpa发送列表作为搜索参数

[英]Spring Data Jpa Send List As a Search Parameter

public class Medicine as Entity
       ...
       ...    
    @Lob
    @ManyToMany
    @JoinTable(name = "Medicine_Ingredients")
    private Set<Ingredient> ingredientList = new HashSet<>();

然后我还有另一个包含成分对象的列表。 我想做的是在存储库中,我想将List作为参数推送,如果其IngredientList包含push List参数,则获取Medicines。

我一直尝试在for循环中迭代对象。 但是我认为这不是一个干净的解决方案。 提前致谢!

更新,此代码阻止了我现在如何处理该问题,但我认为这不是一个干净的解决方案。 到处走走所有来自这里的药物将是一个问题:

medicineRepository.findByTradeNameStartingWithAndManufacturerStartingWith(
                    medicineSearchModel.getTradeName(),
                    medicineSearchModel.getManufacturer()
            )) 

----代码块

        List<Medicine> medicineList = new ArrayList<>();
        ArrayList<String> sub_ingredients = new ArrayList<>();

        for (Ingredient search_ingredient : medicineSearchModel.getIngredientList()) {
            sub_ingredients.add(search_ingredient.getId().toString());
        }
        for (Medicine medicine : medicineRepository.findByTradeNameStartingWithAndManufacturerStartingWith(
                medicineSearchModel.getTradeName(),
                medicineSearchModel.getManufacturer()
        )) {
            ArrayList<String> super_ingredients = new ArrayList<>();
            for (Ingredient medicine_ingredient : medicine.getIngredientList()) {
                super_ingredients.add(medicine_ingredient.getId().toString());
            }
            if (super_ingredients.containsAll(sub_ingredients)) {
                medicineList.add(medicine);
            }
        }
        return medicineList;

您可以尝试一下。 我没有测试此代码,但是做了类似的事情。

注意:
如果ing where ing in不起作用,您可能需要将其更改为ing where ing.id in并准备要设置为参数的配料ID列表。

EntityManager em = ...

Query query = em.createQuery("select m from Medicine m left join fetch m.ingredientList ing where ing in :lstIngredient");
query.setParameter("lstIngredient", yourListParam);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM