我正在使用CriteriaBuilder编写查询,但无法添加order by子句,因为它引发的错误消息ORDER BY 表达式必须出现在选择列表中,这是我的实体。
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.
}
我的查询看起来像这样。
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();
这是hibernate生成的sql。
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
如你看到的。B它不在select子句中。那么,我如何添加B?