如何编写JPA查询,其中参数是一个集合? [英] How to write JPA query where parameter is a set?

查看:1030
本文介绍了如何编写JPA查询,其中参数是一个集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下课程,您如何找到具有特定电子邮件地址的

Assuming the following class, how do you find a Person with a particular email address?

public class Person implements Comparable<Person> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private long id = 0;

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch=FetchType.LAZY)
    private Set<String> email = new HashSet<String>();
}

这样做是否简单,还是有正确的方法?

Is it as simple as doing just this, or is there a proper way?

select p from Person p where p.email=:email


推荐答案

这并不容易。 JPQL为此提供 IN 运算符:

It's not that easy. JPQL provides the IN operator for this:

select p from Person p, IN(p.email) m where m = :email

'旧'方式(读SQL -like)将是:

The 'old' way (read SQL-like) would be:

select p from Person p join p.email m where m = :email

这篇关于如何编写JPA查询,其中参数是一个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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