当前位置: 首页 > 知识库问答 >
问题:

jpa标准api:使用subselect连接

武骁
2023-03-14

此查询用于检索一对多关系中的最后记录(请参阅SQL连接:选择一对多关系中的最后记录)

SELECT  p.*
FROM    customer c 
        INNER JOIN (
                      SELECT customer_id, MAX(date) MaxDate
                      FROM purchase
                      GROUP BY customer_id
                    ) MaxDates ON c.id = MaxDates.customer_id 
        INNER JOIN purchase p ON MaxDates.customer_id = p.customer_id
                    AND MaxDates.MaxDate = p.date;

我的问题是:如何使用jpa criteria api使用subselect构建这个连接?可能吗?如果没有,可以使用jpql吗?

到目前为止我的代码:

final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Purchase> query = cb.createQuery(Purchase.class);
final Root<CustomerEntity> root = query.from(Customer.class);

// here should come the join with the sub-select

final Path<Purchase> path = root.join(Customer_.purchases);
query.select(path);

final TypedQuery<Purchase> typedQuery = entityManager.createQuery(query);
return typedQuery.getResultList();

共有1个答案

段志
2023-03-14

用JPA2。0无法实现这样的查询,但我们可以通过重新构造查询来解决它

SELECT  p.*
FROM    customer c 
        /* This part gets the maximum date of a customer purchase
           We will replace it with a subquery in the where
        INNER JOIN (
                      SELECT customer_id, MAX(date) MaxDate
                      FROM purchase
                      GROUP BY customer_id
                    ) MaxDates ON c.id = MaxDates.customer_id */
        /* This part crosses the maximum date of a customer with the purchase itself to obtain the information
        INNER JOIN purchase p ON MaxDates.customer_id = p.customer_id
                    AND MaxDates.MaxDate = p.date*/
-- We make the crossing with the purchase (there will be N tickets per customer, with N being the number of purchases)
INNER JOIN purchase p on p.customer_id = c.id
-- In the where clause we add a condition so that these N entries become that of the     maximum date
WHERE p.date = (
    SELECT MAX(p2.date)
    FROM purchase p2
    WHERE p2.customer_id = c.id)
;

使用criteria api的实现如下

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Purchase> query = cb.createQuery(Purchase.class);
Root<Customer> root = query.from(Customer.class);
Join<Customer,Purchase> join = root.join(root.get("purchases"),JoinType.INNER);

Subquery<Date> sqMaxdate = cq.subquery();
Root<Purchase> sqRoot = sqMaxDate.from(Purchase.class);
Join<Purchase,Consumer> sqJoin = sqRoot.join(sqRoot.get("customer"),JoinType.INNER)
sqMaxDate.select(cb.max(sqRoot.get("date")));
sqMaxDate.where(cb.equal(sqJoin.get("id"),root.get("id")));

query.where(cb.equal(join.get("date"),sqMaxDate.getSelection()));
query.select(join);

TypedQuery<Purchase> typedQuery = entityManager.createQuery(query);
return typedQuery.getResultList();
 类似资料:
  • 两个数据库表具有外键关系。 它们通过JPA映射到两个实体A和B,但是连接列是从实体中手动删除的,因此在JPA世界中,类A和B是不相关的,您不能通过字段/属性从一个类导航到另一个类。 使用JPA标准API,是否可以创建连接两个表的查询? 我在互联网上找到的所有示例都使用join列来实现目标,但如上所述,它已从代码中删除,因为大多数时候我对A和B之间的关系不感兴趣,而且我担心可能的开销。

  • 尝试编写一个标准api查询。 应该作为参数传递,并且可以更改。我在代码中添加了实体。 感谢任何帮助或指导。 这就是我目前的情况: 我现在能够获取正确的数据,但列表没有排序。这是我在日志中看到的,生成了两个查询,一个有顺序,另一个没有顺序。

  • 主要内容:创建条件查询的步骤,标准API查询子句的方法标准(Criteria)API是构建实体及其持久状态查询的最常用方法之一。 它只是定义JPA查询的另一种方法。 Criteria API定义了一个独立于平台的条件查询,用Java编程语言编写。 它是在JPA 2.0中引入的。 这背后的主要目的是提供一种类型安全的方式来表达查询。 创建条件查询的步骤 要创建标准查询,请按照以下步骤操作: - 第1步: 通过在接口实例上调用方法创建接口的对象。 第2步

  • 我正在尝试实现以下内容: 问:我如何用JPA 2标准实现这个? 我可以单独获得asCount结果,但不知道如何将其加入公司 提前感谢。 PS 1。有一个类似的线程使用HiberNate应用编程接口询问子级计数:HiberNate子级计数标准 2。另一个描述该主题的有用线程: JPA CriteriaBuilder——按一对多关系中关联实体的数量排序

  • 我有两个没有建模关系的表: 带有列的表: 带有列的表 两个表之间的关系是多对一(多通信对一个人C): 通用域名格式。代码=人员C。密码 这两个表用注释映射,但我没有声明任何关系。 我试图选择的是按人员排序的表。说明。 我怎样才能做到JPA和Hibernate?