ITeye博客

zz多对多关系中Set的查询

hfhwan · 2010-03-16 10:36 · Hibernate · 企业架构 · Markdown

一个老师教多个学生,一个学生有多个老师。

class Teacher {
String id;
String name;
Set students;
}

class Student {
String id;
String name;
Set teachers;
}

现在要查询教某一个学生(name为xxx)的老师。

HSQL方法一:(性能最优)
select elements(s.teachers) from Student s where s.name = 'xxx';

HSQL方法二:(要用到 inner join)
select t from Teacher t join t.students s where s.name = 'xxx';

HSQL方法三:(要用到子查询)
select t from Teacher t, Student s where s in elements(t.students) and s.name = 'xxx';

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。