简体   繁体   中英

How do i convert SQL query to hibernate

When i try to run this i get "Unable to locate appropriate constructor on class".

SQL:

select c.*, count(s.student_id)
from class_tbl c    
left join students_tbl s on c.class_id = s.student_id
group by c.class_id 

This is what I tried but seems not to work - HQL:

select new Class(class, count(students.studentId))
from Class as class    
left join class.students as students
group by class.classId  

I have on my object Class a constructor like:

public Class(Class class, long studentCount){
    // [...]
}

First off, don't use Class as a class name, it's a reserved word.

I think your HQL should look like this:

"select new map(class, count(students.studentId) as studentCount) from Class class left 
join fetch class.students students group by class.classId");

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