简体   繁体   中英

How to write Hibernate Criteria for this sql Query?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM