hibernate - Java: How to order by entity.fieldA -


i have problem when querying database , ordering resultset base on entity.fielda.

for example, in school context (student, teacher). have teacher many students.

//student class @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "teacher_id") private teacher teacher; 

and want query sorted teacher.name, teacher available me.

so having query:

from student s order s.teacher; 

it did not work if implement comparable on teacher:

@override public int compareto(teacher o) {   return getname().compareto(o.getname()); } 

my query:

from student left join fetch a.teacher a.xid=:xxx order a.teacher asc 

i have framework that's why i'm not able sort a.teacher.name, i'm asking if a.teacher , implementing comparable interface, sorting a.teacher.name can achieve.

this problem comes lazy loaded datatable, when click entity column sort give entity name not field.

any idea?

hql queries translated sql queries , executed database. , database doesn't know , care classes , compareto() method.

you need

select s student s order s.teacher.name 

read the documentation hql understand how joins work.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -