我正在尝试运行一个Hibernate Criteria查询来进行比较,其中对象不仅仅是一个原语,但我遇到了一些麻烦。
有问题的Criteria查询:
criteriaInList = session.createSQLQuery("select id from student where user_id = " + userID + " and school_id = " + schoolID + " and year = " + year).list();
criteria = session.createCriteria(StudentInteraction.class, "si");
criteria.add(Restrictions.in("si.student.id", criteriaInList));
criteria.add(Restrictions.eq("si.interaction_type.id", 6));
criteria.add(Restrictions.eq("si.student_year", year));
criteria.add(Restrictions.eq("si.student.year", year)); // To ensure that the interaction is current make sure the year on the interaction record and the student's grade match.
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.rowCount());
criteria.setProjection(projectionList);
出于某种原因,使用这种语法很好:
criteria.add(Restrictions.in("si.student.id", criteriaInList));
但是我一加上:
criteria.add(Restrictions.eq("si.student.year", year));
它引发了以下异常:
org.hibernate.QueryException: could not resolve property: student.year of: cas.models.StudentInteraction
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:96)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:102)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at cas.persistence.GenericDaoHibernateImpl.getStudentStats(GenericDaoHibernateImpl.java:2063)
StudentInteraction类:
public class StudentInteraction implements Comparable<StudentInteraction> {
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private ListInteraction interaction_type;
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private Student student;
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private GroupInteraction group;
@Id
@GeneratedValue
private int id;
@Column(name="date")
private Date date;
@Column(name="topic")
private String topic;
@Column(name="notes", length=1000)
private String notes;
@Column(name="student_year") // The year the student is/was in when the interaction was recorded
private int student_year;
... Getters and Setters ...
}
学生班:
public class Student {
@Id
@GeneratedValue
private Integer id;
@Column(name="first_name")
private String first_name;
@Column(name="last_name")
private String last_name;
@Column(name="middle_initial")
private String middle_initial;
@Column(name="dob")
private Timestamp dob;
@Column(name="year")
private Integer year;
@Column(name="notes")
private String notes;
@Column(name="gender", columnDefinition = "int default 0")
private Integer gender; // 1 for Male 2 for Female
@Column(name="tracking_number")
private String tracking_number;
... Getters and setters omitted ...
}
提前感谢您的帮助。
答案 0 :(得分:0)
您需要加入:
criteria.createAlias("si.student", "student");
criteria.add(Restrictions.eq("student.year", year));