jpa2 CriteriaBuilder order by “ORDER BY expressions must appear in select list”

心已入冬 提交于 2019-12-10 21:08:19

问题


I'm writing a query with CriteriaBuilder, but it has not been possible to add the order by clause, because an error it's thrown with the message ORDER BY expressions must appear in select list this are my entities.

public class A{
  Integer aId;
  @ManyToOne
  @JoinColumn(name = "bId", nullable = false)
  B       classB;
  //setter and getter.
}

public class B{
    Integer bId;
    String name;
    //setter and getter.

}

my query look like this.

CriteriaBuilder cb = super.getEntityManager().getCriteriaBuilder();
        CriteriaQuery<A> cq = cb.createQuery(A.class);
        Root<A> aRoot = cq.from(A.class);
        Join<A, B> joinB = aRoot.join("b", JoinType.INNER);

        cq.distinct( true );

        cq.orderBy( cb.asc( joinB.get("name") ));

        return super.getEntityManager().createQuery(cq).getResultList();

This is the sql tha hibernate generates.

select
    distinct a0_.aId as aId,
    a0_.bId as bId,
from
    a a0_ 
inner join
    b b2_ 
        on a0_.aId=b2_.bId 
order by
    b2_.name asc //this is where the error comes

As you can see. B it is not in the select clause. so, how do i add B?

来源:https://stackoverflow.com/questions/28328866/jpa2-criteriabuilder-order-by-order-by-expressions-must-appear-in-select-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!