关系说明:
一个老师有多个学生,一个学生也可以有多个老师;(用第三方表实现)
查询老师下面学生的名字
实体类:
public class Student {
private int sid;
private String sname;
private Set<Teacher> tea =new HashSet<Teacher>();
}
public class Teacher {
private int tid;
private String tname;
private Set<Student> stu = new HashSet<Student>();
}
@Test //查询老师下面的学生姓名
public void testHQL3(){
// t.stu 表示老师下面学生的集合
String hql = "select s from Teacher t join t.stu s where t.tid=:tid";
Query query = session.createQuery(hql);
query.setInteger("tid", 1);
List<Student> ts = query.list();
for (Student student : ts) {
System.out.println(student.getSname());
}
}
4496

被折叠的 条评论
为什么被折叠?



