开发者

How to find using JPQL if member value is in a collection of values

开发者 https://www.devze.com 2023-03-16 14:45 出处:网络
I have an object in which a member is an enum, and I want to write a query returning all elements for which that member is in a list of values. So, I\'ve written the following JQP query

I have an object in which a member is an enum, and I want to write a query returning all elements for which that member is in a list of values. So, I've written the following JQP query

@NamedQuery(name = MyBean.FIND_BY_STATUSES, query = "select t from "+MyBean.TABLE+" t where t.status member of :statuses"),
class MyBean {
     @Enumerated(EnumType.STRING)
     private MyEnum status;
}

That I try to get using the following EJB code

    Query findByStatuses = getEntityManager().createNamedQuery(MyBean.FIND_BY_STATUSES);
    findByStatuses.setParameter("statuses", Arrays.asList(statuses));
    return findByStatuses.getResultLi开发者_JAVA百科st();

Unfortunatly, Glassfish endlessly tells me I'm wrong (which I am, obviously). But what do I have to fix ? and how ?


First your query references to table name, so it tries to be kind of SQL, but maybe database does not have "MEMBER OF"-operator which would be suitable for this kind of use, so let's try with JPQL. There we cannot use MEMBER OF, because it takes path expression as an argument, and that is not what we provide in setParameter. Instead we use IN:

@NamedQuery(name = MyBean.FIND_BY_STATUSES, query = "select t from MyBean t where t.status in(:statuses)")

No need to modify part where you run query.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号