问题
I have a Query Like This
Query
SELECT attendance_entry.roll_no,attendance_entry.absent_date,attendance_entry.absent_status,attendance_entry.leave_status,attendance_entry.od_status
FROM attendance,attendance_entry
WHERE attendance.class_id='"+classId+"' &&
attendance.section_id='"+sectionId+"' &&
attendance_entry.absent_date= STR_TO_DATE('"+date+"','%a %b %d %H:%i:%s IST %Y')
GROUP BY attendance_entry.roll_no";
I need to write criteria to Parse Json
I tried with following Criteria
Criteria cr = getSession().createCriteria(AttendanceEntry.class)
.setProjection(Projections.projectionList()
.add(Projections.property("rollno"),"rollno")
.add(Projections.property("absent"),"absent")
.add(Projections.property("leave"),"leave")
.add(Projections.property("od"),"od")
.add(Projections.groupProperty("rollno"),"rollno"))
.add(Restrictions.eq("attendance.classYear.id", classId))
.add(Restrictions.eq("attendance.section.id", sectionId))
.add(Restrictions.eq("absentDate", date))
.setResultTransformer(Transformers.aliasToBean(AttendanceEntry.class));
return cr.list();
and i having the Following Error
Error
could not resolve property: attendance.classYear.id of: com.technofolks.model.AttendanceEntry; nested exception is org.hibernate.QueryException: could not resolve property: attendance.classYear.id of: com.technofolks.model.AttendanceEntry
I have mapped attendance and Entry as onetomany and manytoone. How to Write Criteria Query?
回答1:
This Criteria Works for my MySQL Query
@Override
@Transactional
@SuppressWarnings("unchecked")
public List<AttendanceEntry> getAttendanceByDateSearch(String classId, String sectionId, Date date) {
Criteria cr = getSession().createCriteria(AttendanceEntry.class,"attendanceEntry")
.createAlias("attendanceEntry.attendance","attendance")
.setProjection(Projections.projectionList()
.add(Projections.property("rollno"),"rollno")
.add(Projections.property("absent"),"absent")
.add(Projections.property("leave"),"leave")
.add(Projections.property("od"),"od")
.add(Projections.property("absentDate"),"absentDate")
.add(Projections.groupProperty("rollno"),"rollno"))
.add(Restrictions.eq("attendance.classYear.id", classId))
.add(Restrictions.eq("attendance.section.id", sectionId))
.add(Restrictions.eq("absentDate", date))
.setResultTransformer(Transformers.aliasToBean(AttendanceEntry.class));
return cr.list();
}
来源:https://stackoverflow.com/questions/28625811/how-to-write-hibernate-criteria-for-this-sql-query