JPA 2 No explicit selection and an implicit one cold not be determined

后端 未结 2 1302
自闭症患者
自闭症患者 2021-01-01 21:17

I am trying to fetch all users for a folder where the user was created after a certain date. the relationship between the user and the folder lives in a seperate table.

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 21:30

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

    CriteriaQuery query = builder.createQuery();
    Root men = query.from( Person.class );
    Root 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 select the root. Note I had to create one root to solve this. Here is my example:

    CriteriaQuery query = builder.createQuery();
    Root 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 I can't figure out is why an implicit selection could not be made. In Hibernate's example the only class that is used is Person.class. I'll update my response when I dig in a little further.

提交回复
热议问题