如何子查询 JPA 2.0 中多个键值组合的映射?

Posted

技术标签:

【中文标题】如何子查询 JPA 2.0 中多个键值组合的映射?【英文标题】:How do I subquery a map for multiple key value combinations in JPA 2.0? 【发布时间】:2016-07-15 13:16:16 【问题描述】:

我有以下实体(示例):

@Entity
@Table(name = "person")
public class Person implements Serializable 

  @Id
  @Column(name = "person_id", columnDefinition = "UUID")
  private UUID userId;

  @Column(name = "name")
  private String name;


  @ElementCollection
  @MapKeyColumn(name = "phonetype")
  @Column(name = "number")
  @CollectionTable(name = "person_phones", joinColumns = @JoinColumn(name = "userId"))
  private Map<String, String> phoneNumbers;


现在,phoneNumbers 是 String,在这个例子中是 String。假设键是类型(如“mobile”、“home”、“office”、“fax”、“pager”...),值是任何文本格式的实际数字。

我想查询一个有两个电话号码的人:

Select * From person where
   in his phone_numbers exists phonetype = 'home' and number = '0-123-456'
   and also in his phone_numbers exists phonetype = 'mobile' and number = '9-876-421'
   (and possibly, dynamically others)
   and name = 'John'

我已经构建了一个有效的 sql 子查询:

select home.userId from
(
    (SELECT userId from person_phones
    where (phonetype = 'home' and number = '0-123-456'))
) as home,
(
    (SELECT userId from person_phones
    where (phonetype = 'mobile' and number = '9-876-421'))
) as mobile
where home.userId = mobile.userId

如前所述,这只是一个 sql 子查询。我正在我的项目中编写 JPA 2.1 条件查询。这似乎异常复杂。谁能给个提示?

【问题讨论】:

【参考方案1】:

有一个类似的问题,使用多个内连接而不是子查询来解决它。

EntityManagerFactory emf =  Persistence.createEntityManagerFactory("Person-Test");
    EntityManager em = emf.createEntityManager();

    Map<String, String> phoneNumbers = new HashMap<>();
    phoneNumbers.put("home","0-123-456");
    phoneNumbers.put("mobile","9-876-421");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Person> query = cb.createQuery(Person.class);
    Root<Person> personRoot = query.from(Person.class);
    query.select(personRoot);

    phoneNumbers.forEach((k, v) -> 
        MapJoin<Person, String, String> phoneNrJoinJoin = personRoot.joinMap("phoneNumbers");
        phoneNrJoinJoin.on(cb.equal(phoneNrJoinJoin.key(), k), cb.equal(phoneNrJoinJoin.value(), v));
    );

    query.where(cb.equal(personRoot.get("name"), "John"));

    List<Person> people = em.createQuery(query).getResultList();

这会导致以下休眠查询(为清楚起见重命名别名)

SELECT person.person_id, person.name
FROM person 
INNER JOIN person_phones a
ON person.person_id = a.userid
AND (a.phonetype = ? AND a.NUMBER = ?) 
INNER JOIN person_phones b
on person.person_id=b.userId 
and (b.phonetype=? and b.number = ? )
WHERE
person.name = ?;

返回所有提到的电话号码匹配的所有类型为 person 的元组。

【讨论】:

以上是关于如何子查询 JPA 2.0 中多个键值组合的映射?的主要内容,如果未能解决你的问题,请参考以下文章

JPA 2.0、标准 API、子查询、表达式中

映射异常:在 JPA 2.0 中外部化命名本机查询时无法读取 XML

对 JP-QL (JPA 2.0) 中的“ElementCollection”映射字段执行“MEMBER OF”查询

如何在唯一的映射或非对象 JPA 中返回多个结果

Spring data jpa - 如何通过方法名称组合多个And和Or

组合主键及JPA映射