gpt4 book ai didi

java - 使用 CriteriaBuilder 从子查询中的路径中选择?

转载 作者:行者123 更新时间:2023-12-02 10:57:14 24 4
gpt4 key购买 nike

CriteriaBuilder 如何用于构建 SELECT a FROM e.attributes a .... 形式的子查询,其中 e 是一些外部查询中引用的实体?

我有一些涉及自由形式键值结构的实体类(这提出了它自己的问题,但这就是我所拥有的)。我需要找到存在某些键值对的实体。我可以将其编写为以下形式的 JPQL 查询:

SELECT e FROM Entity e 
WHERE e.type = 'foo'
AND EXISTS (SELECT a FROM e.attributes a
WHERE a.key = 'bar'
AND a.value = 'baz')

对于固定查询字符串,我可以使用 EntityManager.createQuery() 创建查询:

EntityManager em = /* ... */;
TypedQuery<Entity> tq = em.createQuery(queryString, Entity.class);

实际上,查询中存在多个EXISTS,因此我需要使用 CriteriaBuilder 构建查询。到目前为止,我得到的最接近的是子查询SELECT a from Attributes a WHERE ...,但这当然不限于e.attributes:

CriteriaBuilder cb = em.getCriteriaBuilder();

CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
Root<Entity> root = query.from(Entity.class);

Subquery<Attribute> subquery = query.subquery(Attribute.class);
Root<Attribute> subroot = subquery.from(Attribute.class); // too broad
subquery.select(subroot)
.where(cb.and(//
cb.equal(subroot.get("key"), cb.literal("bar")),
cb.equal(subroot.get("value"), cb.literal("baz"))));

query.select(root)
.where(cb.and(//
cb.equal(root.get("type"), cb.literal("foo")), //
cb.exists(subquery)));

子查询上有许多 correlate() 方法,我想知道是否需要在外部查询中加入实体及其属性,然后以某种方式关联(),但我从 EE7 javadocs 中不确定相关性到底是什么(但 correlate() 确实返回一个 From,这意味着我可以从中进行 SELECT,这是有希望的)。

最佳答案

我最终在JPA 2.0 spec中找到了这个问题的答案。 。第 276 页有一个相关查询的示例:

Example 4: A Special case

In order to express some correlated subqueries involving unidirectional relationships, it may be useful to correlate the domain of the subquery with the domain of the containing query. This is performed by using the correlate method of the Subquery interface.

For example:

CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> customer = q.from(Customer.class);
Subquery<Long> sq = q.subquery(Long.class);
Root<Customer> customerSub = sq.correlate(customer);
Join<Customer,Order> order = customerSub.join(Customer_.orders);
q.where(cb.gt(sq.select(cb.count(order)), 10))
.select(customer);

This query corresponds to the following Java Persistence query language query:

SELECT c
FROM Customer c
WHERE (SELECT COUNT(o) FROM c.orders o) > 10

让我困惑了一段时间,并让我思考也许我的 JPQL 可能不合法的是,FROM 子句的语法(同一规范中的第 173 页)给出如下:

from_clause ::=  FROM  identification_variable_declaration  ...
identification_variable_declaration ::= range_variable_declaration ...
range_variable_declaration ::= entity_name [AS] identification_variable

我不清楚像 e.attributes 这样的东西怎么可能是一个“entity_name”。事实证明,子查询 FROM 子句的语法中实际上存在完全其他的产生式,其中包括集合支持:

subquery_from_clause ::= FROM subselect_identification_variable_declaration ...
subselect_identification_variable_declaration ::=
identification_variable_declaration |
derived_path_expression [AS] identification_variable {join}* |
derived_collection_member_declaration
derived_path_expression ::=
superquery_identification_variable.{single_valued_object_field.}*collection_valued_field |
superquery_identification_variable.{single_valued_object_field.}*single_valued_object_field

关于java - 使用 CriteriaBuilder 从子查询中的路径中选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51632954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com