使用 Criteria API 运行查询时出现异常
org.hibernate.QueryException: could not resolve property: com of: com.data.Collage at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1362)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:204)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:191)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:81)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:58)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
类定义如下
class Collage {
int id;
String collageName;
List lstStudent;
}
class Student{
String studentName;
int id;
}
已完成上述类的映射。
现在我正在尝试在我有 collageID 和 StudentID 的单个查询中获取拼贴名称和学生姓名。为此使用了 Criteria API。
Criteria cr = session.createCriteria("com.data.Collage","collageAlias");
cr.createAlias("com.data.Student","studentAlias");
cr.add(Restrictions.eq("collageAlias.id", "402882c2369bc53901369bc95d5f0137"));
cr.add(Restrictions.eq("studentAlias.id","ff80808134cbe5a10134d14ff20300a9"));
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("collageAlias.collageName"));
properties.add(Projections.property("studentAlias.studentName"));
cr.setProjection(properties);
List collage_student = cr.list();
我尝试使用 Collage.class,删除了 collage 的别名作为标准 API 的默认类,但它没有用。
有什么建议吗?