IN和MEMBER成员之间的区别是什么? [英] What's the difference between the IN and MEMBER OF JPQL operators?

查看:445
本文介绍了IN和MEMBER成员之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IN和成员JPQL运算符之间有什么区别?

What's the difference between the IN and MEMBER OF JPQL operators?

推荐答案

IN tests是单值路径表达式的值(您提供给查询的值(或通过子查询获取的值)的实体的持久属性。

IN tests is value of single valued path expression (persistent attribute of your entity) in values you provided to query (or fetched via subquery).

成员测试是您在实体的某些集合中为查询(或使用表达式定义)值提供的值。

MEMBER OF tests is value you provided to query (or defined with expression) member of values in some collection in your entity.

让我们使用以下示例实体:

Lets's use following example entity:

@Entity
public class EntityA {
    private @Id Integer id;
    private Integer someValue;
    @ElementCollection
    List<Integer> listOfValues;

    public EntityA() { }

    public EntityA(Integer id, Integer someValue, List<Integer> listOfValues) {
        this.id = id;
        this.someValue = someValue;
        this.listOfValues = listOfValues;
    }
}

以下测试数据:

EntityA a1 = new EntityA(1, 1, Arrays.asList(4, 5, 6));
EntityA a2 = new EntityA(2, 2, Arrays.asList(7, 8, 9));

通过以下查询得到a1作为结果,因为它的someValue是其中之一(0,1, 3)。在查询中使用文字( SELECT a FROM EntityA a WHERE a.someValue IN(0,1,3))会产生相同的结果。

With following query we get a1 as result, because it's someValue is one of the (0,1,3). Using literals in query (SELECT a FROM EntityA a WHERE a.someValue IN (0, 1, 3)) produces same result.

TypedQuery<EntityA> queryIn = em.createQuery(
    "SELECT a FROM EntityA a WHERE a.someValue IN :values", EntityA.class);
queryIn.setParameter("values", Arrays.asList(0, 1, 3));
List<EntityA> resultIn = queryIn.getResultList();

使用以下查询得到a2作为结果,因为7是listOfValues中的值之一:

With following query we get a2 as result, because 7 is one of the values in listOfValues:

TypedQuery<EntityA> queryMemberOf = em.createQuery(
    "SELECT a FROM EntityA a WHERE :value MEMBER OF a.listOfValues", EntityA.class);
queryMemberOf.setParameter("value", 7);
List<EntityA> resultMemberOf = queryMemberOf.getResultList();

此功能(包括作为参数的集合)在JPA 2.0规范中定义,并不特定于Hibernate(上面的代码适用于例如EclipseLink)。

This functionality (including collection as parameter) is defined in JPA 2.0 specification and is not specific to Hibernate (above code works for example with EclipseLink).

这篇关于IN和MEMBER成员之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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