0
votes

I am rather new to JPA and it's CriteriaBuilder / CriteriaQuery API:

I want to query an extract of a set of Objects(as you can see below, i only select element n to m of the results)

The query itsself works well and I get the results i expect.

But if i try to count the whole amount of possible results, i get a java.lang.IllegalArgumentException:

org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias1.prop1' [select count(generatedAlias0) from com.myCompany.blablub.MyClass as generatedAlias0 where ( 1=1 ) and ( generatedAlias1.prop1=:param0 )]"

final CriteriaQuery<MyClass> criteriaQuery = builder.createQuery(MyClass.class);
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    countQuery.select(builder.count(countQuery.from(MyClass.class)));

    Long totalCount = -1L;
    List<MyClass> MyClassList;

    Root<MyClass> root = criteriaQuery.from(MyClass.class);



    if(myClassFilter != null) {         

            List<Predicate> predicates = new ArrayList<Predicate>();

            if(myClassFilter.getProp1() != null) {
                Join<MyClass, MyOtherClass> myJoin1 = root.join(MyClass_.prop1);
                predicates.add(builder.equal(myJoin1.get(MyOtherClass_.blah), myClassFilter.getProp1() ));
            }

            if(myClassFilter.getProp2() != null) {
                predicates.add(builder.equal(root.get(MyClass_.blubb), myClassFilter.getProp2() ));
            }

            if(myClassFilter.getProp3() != null) {
                Join<MyClass, MyOtherClass> myJoin2 = root.join(MyClass_.foo);
                predicates.add(builder.equal(myJoin2.get(MyOtherClass_.bar), myClassFilter.getProp3() ));
            }


            if (myClassFilter.getSpecialPropList() != null) { 
                if (myClassFilter.getSpecialPropList().length>0) {
                        Predicate orPredicates = builder.disjunction();
                        for (String specialProp : myClassFilter.getSpecialPropList()) {
                            Join<MyClass, specialProp> specialPropJoin = root.join(MyClass_.specialProps);
                            Predicate newPredicate = builder.equal(specialPropJoin.get(specialProp_.dudum), specialProp);
                            orPredicates = builder.or(orPredicates, newPredicate);
                        }
                        predicates.add(orPredicates);
                }
            }

            Predicate finalPredicate = builder.conjunction();

            for(Predicate p: predicates) {
                finalPredicate = builder.and(finalPredicate, p);
            }

            criteriaQuery.select(root).where(finalPredicate);
            countQuery.where(finalPredicate);
    }

    count = entityManager.createQuery(countQuery).getFirstResult();

    TypedQuery<MyClass> typedQuery = entityManager.createQuery(criteriaQuery);

    if(first >= 0) {
        typedQuery.setFirstResult(first);
    }
    if(count > 0) {
        typedQuery.setMaxResults(count);
    }

    MyClassList = typedQuery.getResultList();

I hope you can give me a hint on what im doing wrong.

I think i see where my problem is.

The behaviour only apprears when i use a join.

a query without join looks like this:

select count(generatedAlias0) from MyClass as generatedAlias0 where ( 1=1 ) and ( generatedAlias0.blubb=:param0 )

but when i want to use a join it looks like this:

select count(generatedAlias0) from MyClass as generatedAlias0 where ( 1=1 ) and ( generatedAlias1.blah=:param0 )

while the query without count looks like this:

select generatedAlias0 from MyClass as generatedAlias0 inner join generatedAlias0.prop1 as generatedAlias1 where ( 1=1 ) and ( generatedAlias1.blah=:param0 )

it obviously doesnt know what generatedAlias1 is because the whole join is missing. does anyone know how i can fix this behaviour?

1

1 Answers

0
votes

You create a join using the root of the criteriaQUery. You need to create a join using the root of your countQuery as well.

Root<MyClass> countRoot = countQuery.from(MyClass.class);
Join<MyClass, MyOtherClass> mycountJoin = countRoot.join(MyClass_.foo);