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

时间:2017-08-23 08:33:46

标签: java spring hibernate spring-data-jpa hql

echo GridView::widget([
     'dataProvider'=> $dataProvider,
     'columns' => [

        ['class' => 'kartik\grid\SerialColumn'],

         ... // Some Parameters

         [  
            'label' => Yii::t('app', 'Status'),
            'format' => 'raw',
            'value' => function ($model) {

            /* ******** Working fine *******/
            /*
             return Html::activeDropDownList( $model, 'jaStatus',  
                   [ 1 =>'Submitted', 2 =>'Processed', 3 =>'Approved', 
                     4 =>'Declined'
                   ],['onchange' => 'updateApplicationStatus()']);
           */

            /* ******** Don't work *******/                  
            return Select2::widget([
                    'model' => $model,
                    'attribute' => 'jaStatus',
                    'data' => [ 1 =>'Submitted', 2 =>'Processed', 
                                3 =>'Approved', 4 =>'Declined'
                               ],
                    'hideSearch' => true,
                    'pluginOptions' => [
                             'allowClear' => false,
                    ],
            ]);
            }
        ],

       [
        'class' => '\kartik\grid\ActionColumn',
        'header' => 'Actions',
        'template' => '{view}',
        'buttons' => [
                        ...
         ],

        'urlCreator' => function ($action, $model, $key, $index) {
                                ...
                        }

      ]

    ],

    ... // some settings
]);

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

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

更新,此代码阻止了我现在处理问题的方法,但我认为它不是一个干净的解决方案。来自这里的所有药物都会出现问题:

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

----代码块

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

1 个答案:

答案 0 :(得分:0)

你可以试试这个。我没有测试这段代码,但我做了类似的事情。

注意:
如果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);