[ACCEPTED]-JPA 2 No explicit selection and an implicit one cold not be determined-criteria-api

Accepted answer
Score: 31

You should set explicitly selection also 1 for "subqueries".

rpSubQ.select(subQRoot);
Score: 8

I had the exact same error today. The funny 9 thing is that I grabbed my example from 8 Hibernate 3.6.3.Final docs. Their example 7 is:

CriteriaQuery query = builder.createQuery();
Root<Person> men = query.from( Person.class );
Root<Person> women = query.from( Person.class );
Predicate menRestriction = builder.and(
    builder.equal( men.get( Person_.gender ), Gender.MALE ),
    builder.equal( men.get( Person_.relationshipStatus ), RelationshipStatus.SINGLE )
);
Predicate womenRestriction = builder.and(
    builder.equal( women.get( Person_.gender ), Gender.FEMALE ),
    builder.equal( women.get( Person_.relationshipStatus ), RelationshipStatus.SINGLE )
);
query.where( builder.and( menRestriction, womenRestriction ) );

What I did to "fix" the error is explicitly 6 select the root. Note I had to create one 5 root to solve this. Here is my example:

CriteriaQuery query = builder.createQuery();
Root<Person> personRoot = query.from( Person.class );
Predicate menRestriction = builder.and(
    builder.equal( personRoot.get( Person_.gender ), Gender.MALE ),
    builder.equal( personRoot.get( Person_.relationshipStatus ), RelationshipStatus.SINGLE )
);
Predicate womenRestriction = builder.and(
    builder.equal( personRoot.get( Person_.gender ), Gender.FEMALE ),
    builder.equal( personRoot.get( Person_.relationshipStatus ), RelationshipStatus.SINGLE )
);
query.select(personRoot);
query.where( builder.and( menRestriction, womenRestriction ) );

What 4 I can't figure out is why an implicit selection 3 could not be made. In Hibernate's example 2 the only class that is used is Person.class. I'll update 1 my response when I dig in a little further.

Score: 2

Query roots specify the domain objects on which 4 the query is evaluated. Query root is an 3 instance of the Root interface and must 2 explicitly declared, so instead of this

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(type);
list.forEach(i -> entityManager
         .createQuery(criteriaQuery.where(
              criteriaBuilder.equal(criteriaQuery.from(type).get(field), i))).getResultList());

You 1 must explicitly define root as

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(type);
Root<T> root = criteriaQuery.from(type);
list.forEach(i -> entityManager
        .createQuery(criteriaQuery.where(
            criteriaBuilder.equal(root.get(field), i))).getResultList());

More Related questions