I need a criteria query for the following SQL query.
SELECT w.weight_id, w.weight, zc.charge
FROM weight w
LEFT OUTER JOIN zone_charge zc ON w.weight_id=zc.weight_id
AND zc.zone_id=? <-------
ORDER BY w.weight ASC
The corresponding JPQL query would be like,
SELECT w.weightId, w.weight, zc.charge
FROM Weight w
LEFT JOIN w.zoneChargeSet zc
WITH zc.zone.zoneId=:id <-------
ORDER BY w.weight
I can't reproduce the same with criteria especially WITH zc.zone.zoneId=:id
.
The following criteria query uses the where
clause.
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple>criteriaQuery=criteriaBuilder.createTupleQuery();
Root<Weight> root = criteriaQuery.from(entityManager.getMetamodel().entity(Weight.class));
SetJoin<Weight, ZoneCharge> join = root.join(Weight_.zoneChargeSet, JoinType.LEFT);
ParameterExpression<Long>parameterExpression=criteriaBuilder.parameter(Long.class);
criteriaQuery.where(criteriaBuilder.equal(join.get(ZoneCharge_.zoneTable).get(ZoneTable_.zoneId), parameterExpression));
criteriaQuery.multiselect(root.get(Weight_.weightId), root.get(Weight_.weight), join.get(ZoneCharge_.charge));
criteriaQuery.orderBy(criteriaBuilder.asc(root.get(Weight_.weight)));
TypedQuery<Tuple> typedQuery = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, 1L);
List<Tuple> list = typedQuery.getResultList();
How could it be modified so that it corresponds to ...LEFT OUTER JOIN zone_charge zc ON w.weight_id=zc.weight_id
AND zc.zone_id=?
This generates the following SQL query.
SELECT weight0_.weight_id AS col_0_0_,
weight0_.weight AS col_1_0_,
zonecharge1_.charge AS col_2_0_
FROM social_networking.weight weight0_
LEFT OUTER JOIN social_networking.zone_charge zonecharge1_
ON weight0_.weight_id = zonecharge1_.weight_id
WHERE zonecharge1_.zone_id =?
ORDER BY weight0_.weight ASC