使用Hibernate Criteria将Projection字段设置为hibernate中的外键列

时间:2014-04-18 08:23:23

标签: java hibernate hibernate-criteria

如何在hibernate条件查询中将投影字段设置为外键引用。例如,

 Criteria crt = s.createCriteria(UsersforGrades.class).createCriteria("id", "id1").createCriteria("id1.userId", "id2").add(Restrictions.eq("id2.userId", ue.getUserId()));
                ProjectionList proList = Projections.projectionList();
                proList.add(Projections.property("gradeId.gradeId"));

例外:

org.hibernate.QueryException: could not resolve property: gradeId of: com.treamis.entity.UserEntity

1 个答案:

答案 0 :(得分:1)

迟到的回答虽然:(想出相同的情景)

在这种情况下,您需要创建别名。

criteria.createAlias("gradeId", "gradeId");
criteria.setProjection(Projections.property("gradeId.gradeId"));