如何为这个SQL查询编写Hibernate Criteria?

时间:2015-02-20 09:42:02

标签: java mysql json hibernate hibernate-criteria

我有像这样的查询

查询

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";

我需要为Parse Json编写标准

我尝试了以下标准

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(); 

我有以下错误

错误

 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

我已将考勤和参赛作为onetomany和manytoone。如何编写标准查询?

1 个答案:

答案 0 :(得分:2)

此标准适用于我的MySQL查询

@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(); 
}