use of com.blazebit.persistence.impl.SubqueryBuilderListenerImpl in project blaze-persistence by Blazebit.
the class SizeTransformationVisitor method generateSubquery.
private SubqueryExpression generateSubquery(PathExpression sizeArg) {
JoinNode sizeArgJoin = (JoinNode) sizeArg.getBaseNode();
final Type<?> nodeType = sizeArgJoin.getNodeType();
if (!(nodeType instanceof EntityType<?>)) {
throw new IllegalArgumentException("Size on a collection owned by a non-entity type is not supported yet: " + sizeArg);
}
final EntityType<?> startType = (EntityType<?>) nodeType;
Subquery countSubquery = (Subquery) subqueryInitFactory.createSubqueryInitiator(null, new SubqueryBuilderListenerImpl<>(), false, getClause()).from(sizeArg.getPathReference().toString()).select("COUNT(*)");
for (SingularAttribute<?, ?> idAttribute : JpaMetamodelUtils.getIdAttributes(startType)) {
String groupByExprString = sizeArgJoin.getAlias() + "." + idAttribute.getName();
ResolvedExpression groupByExpr = new ResolvedExpression(groupByExprString, null);
Set<ClauseType> clauseTypes = subqueryGroupBys.get(groupByExpr);
if (clauseTypes == null) {
List<PathElementExpression> pathElementExpressions = new ArrayList<>(2);
pathElementExpressions.add(new PropertyExpression(sizeArgJoin.getAlias()));
pathElementExpressions.add(new PropertyExpression(idAttribute.getName()));
PathExpression pathExpression = new PathExpression(pathElementExpressions);
pathExpression.setPathReference(new SimplePathReference(sizeArgJoin, idAttribute.getName(), metamodel.type(JpaMetamodelUtils.resolveFieldClass(startType.getJavaType(), idAttribute))));
groupByExpr = new ResolvedExpression(groupByExprString, pathExpression);
subqueryGroupBys.put(groupByExpr, EnumSet.of(clause));
} else {
clauseTypes.add(clause);
}
}
return new SubqueryExpression(countSubquery);
}
Aggregations