I want to display the names which are stored in the customer's profiles when showing the customer's orders. I have tried to write the query in pgAdmin and I show the data as I want.
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;
The problem is that I am not able to translate the above query to JPA criteria because of subquery on P.user_id = C.id. I have tried to find a similar question in the forum or google but I can't find a good example on this case. This is the current translation I have.
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))
Can you help to complete it?
Thanks in advance