Using a ParameterExpression as part of the In clause

前端 未结 1 1995
轮回少年
轮回少年 2021-01-24 10:35

I would like to use a parameter expression as part of an in clause. I would like to query for a list of Foos that have a Bar that is in a set of Bars. Is this possible?

1条回答
  •  再見小時候
    2021-01-24 11:14

    With in expressions you can only use primitive comparison types, so you need to make a join and compare a field of a primitive type (here I have used Integer id):

    Root foo = cq.from(Foo.class);
    Join bar = foo.join(Foo_.bar);
    ParameterExpression bars = cb.parameter(Collection.class);
    cq.where(bar.get(Bar_.id).in(bars));
    TypedQuery tq = em.createQuery(cq);
    Collection barsParameter = new ArrayList ();
    barsParameter.add(1);
    List resultList = tq.setParameter(bars, barsParameter).getResultList();
    

    0 讨论(0)
提交回复
热议问题