一個老師教許多學生,一個學生被許多老師教,一個學生有好多書,同一種書被許多同學擁有.
要查詢教擁有書"a"的學生的老師!
HQL如何寫呀?如何取值?
class Teacher{
String id;
String name;
Set students;
}
class Student{
String id;
String name;
Set teachers;
Set books;
}
class Book{
String id;
String name;
Set students;
}
String id;
String name;
Set students;
}
class Student{
String id;
String name;
Set teachers;
Set books;
}
class Book{
String id;
String name;
Set students;
}
<class name="net.fengrun.lzk.stu.Book" table="books">
<id name="id" type="integer" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" table="student_book" inverse="true" cascade="none" lazy="true">
<key column="bookid"/>
<many-to-many column="studentid" class="net.fengrun.lzk.stu.Student"/>
</set>
</class>
<class name="net.fengrun.lzk.stu.Student" table="students">
<id name="id" type="integer" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="books" table="student_book" inverse="false" cascade="none" lazy="true">
<key column="studentid"/>
<many-to-many column="bookid" class="net.fengrun.lzk.stu.Book"/>
</set>
<set name="teachers" table="teacher_student" inverse="false" cascade="none" lazy="true">
<key column="studentid"/>
<many-to-many column="teacherid" class="net.fengrun.lzk.stu.Teacher"/>
</set>
</class>
<class name="net.fengrun.lzk.stu.Teacher" table="teachers">
<id name="id" type="integer" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" table="teacher_student" inverse="true" cascade="delete-orphan" lazy="true" >
<key column="teacherid"/>
<many-to-many column="studentid" class="net.fengrun.lzk.stu.Student"/>
</set>
</class>
<id name="id" type="integer" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" table="student_book" inverse="true" cascade="none" lazy="true">
<key column="bookid"/>
<many-to-many column="studentid" class="net.fengrun.lzk.stu.Student"/>
</set>
</class>
<class name="net.fengrun.lzk.stu.Student" table="students">
<id name="id" type="integer" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="books" table="student_book" inverse="false" cascade="none" lazy="true">
<key column="studentid"/>
<many-to-many column="bookid" class="net.fengrun.lzk.stu.Book"/>
</set>
<set name="teachers" table="teacher_student" inverse="false" cascade="none" lazy="true">
<key column="studentid"/>
<many-to-many column="teacherid" class="net.fengrun.lzk.stu.Teacher"/>
</set>
</class>
<class name="net.fengrun.lzk.stu.Teacher" table="teachers">
<id name="id" type="integer" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" table="teacher_student" inverse="true" cascade="delete-orphan" lazy="true" >
<key column="teacherid"/>
<many-to-many column="studentid" class="net.fengrun.lzk.stu.Student"/>
</set>
</class>
Hql語句:
SELECT t FROM Teacher t join t.students s join s.books b where b.name = 'a'
解釋:t.students s中s並不是集合的意思,而是t的students對象的表別名,join t.students s這個hql,hibernate會翻譯成兩個表的內連接關係
錯誤寫法:
SELECT t FROM teacher t where t.students.books.name = 'a'
其實道理是很顯然的,t.students是一個Set,那麼這個Set怎麼可能有books這樣的屬性呢?同理books.name也不對,所以使用表間連接並給出別名t.students s,此時的對象s纔有books屬性,所以可以寫s.books b,最後使用b.name限制查詢b.name = 'a'.
另外一種寫法:
SELECT t FROM Teacher t,Student s,Book b where s.id in elements(t.students) and b.id in elements(s.books)
這種方法沒有出錯!不過這種方式要用子查詢! 但是MySql並不支持子查詢,爲了通用性,選擇第一種做法。
未完,待續...