如何在join子句上编写JPA子查询条件

时间:2016-05-01 15:56:30

标签: hibernate jpa

我希望在显示客户订单时显示存储在客户配置文件中的名称。我试图在pgAdmin中编写查询,然后按我的意愿显示数据。

Select O, S.id as sid, PR.title, C.email, P.name
From orders O
inner join subscription S on o.subscription_id = S.id
inner join product PR on S.product_id = PR.id 
inner join account C on S.account_id = C.id
inner join profile P on (P.user_id = C.id)
where PR.merchant_id = :merchantId;

问题是由于P.user_id = C.id上的子查询,我无法将上述查询转换为JPA标准。我试图在论坛或谷歌中找到类似的问题,但我找不到这个案例的好例子。这是我目前的翻译。

final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<CusODTO> query = builder.createQuery(CusODTO.class);
Root<Order> rootOrder = query.from(Order.class);
Join<Order, Subscription> joinSub = rootOrder.join(Order_.subscription);
Join<Subscription, Product> joinPro = joinSub.join(Subscription_.product);
Join<Subscription, Customer> joinCus = joinSub.join(Subscription_.customer);

// TODO implement subquery to get joinCustomer profile

query.select(builder.construc(
    CusODTO.class
    , rootOrder
    , joinSub.get(Subscription_.id)
    , joinPro.get(Product_.title))
    , joinCus.get(Customer_.email)
    , "nullName"             // should select joinCustomer profile name
    )
).where(buider.equal(joinPro.get(Product_.merchant), merchant))

你能帮忙完成吗?

提前致谢

0 个答案:

没有答案