How to write Hibernate Criteria for this sql Query?

ぃ、小莉子 提交于 2020-01-03 03:20:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!