---
title: "zz多对多关系中Set的查询"
author: "hfhwan"
date: "2010-03-16T10:36:30Z"
tags: []
categories: ["企业架构"]
slug: "zz-set"
draft: false
lastmod: "2010-03-16T10:36:30Z"
id: 616769
url: "https://hfhwan.iteye.com/blog/616769"
---

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

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';