hibernate中left join问题?

来源:百度知道 编辑:UC知道 时间:2024/06/13 22:14:29
Teacher与Student为one-to-many
Teacher
id
name
sex
age
Student
id
teacherId
name
age
我现在想实在如下sql语句,
select * from Student left join Teacher on Student.teacherId=Teacher.id and Student.age<Teacher.age
请问hibernate如何配置?
附:我主要不清楚的就是left join on后面跟多个条件的情况..and ...,
如select * from Student left join Teacher on Student.teacherId=Teacher.id,这种情况,我都会配置,
请教,谢谢。
To: zjb_dill - 经理 四级
left join 时 on中的条件与where中的条件是有区别的吧?

你是想问HQL中多个链接条件怎么写?还是SQL怎么写?(下面我说的,前提是我认为你分清了on xxx and xxx where xxx xxx中 on和where区别)
作为链接条件,hql好像没须要用户手动加上on。因为Student和Teacher对象的关系已经用户预先设定了。如每个学生对象内部都有一个教师类型的属性并引用了一个教师对象。
如果你非要在hql中加入on,自己手动试试,我没试过,好像HQL没有ON关键字的支持。就算不加,HQL内部操作的SQL会自动加上的。用户根本不需要设定链接条件,所以你想把Student.age<Teacher.age 作为链接条件,倒不如作为查询条件。
放在在where后面 where xxx=xxx and xxx=dsfds and sdfsd=sdfdsfsd
同样也能达到效果

HQL不支持ON关键字,既然已经是one-to-many的关系了,那就是这两个model对象已经通过ID建立起关系了,查询student时要连带查出teacher,可以这样写

from Student stu left join stu.teacher teacher where stu.age<teacher.age