【一对多:班级对学生】
grade:
实体:持久化类
private Set<Student> stu; //set集合,每一个班级对应的多个学生的集合
映射文件中:
<set name="stu" lazy="false">
<key column="gid"></key> //外键学生表 班级字段
<one-to-many class="com.baidu.entity.Student"/>
</set>
【dao】
public Grade getGradeList(int gid){
Configuration cf = new Configuration().configure();
SessionFactory sf = cf.buildSessionFactory();
Session s = sf.openSession();
Grade g = (Grade)s.get(Grade.class, gid); //
s.close();
return g;
}
【test】
public void testGrade(){
QueryDao dao = new QueryDao();
Grade g = dao.getGradeList(2); //获取2班的实体
Set<Student> stu = g.getStu(); //从实体里获取学生集合
for(Student x:stu){
System.out.println(x.getSname());
}
}
==============================================================
【多对一:学生对班级】
student:
持久化类:
private Grade grade;
<many-to-one name="grade" class="com.baidu.entity.Grade" column="gid" lazy="false"/>
或者写成:
<many-to-one name="grade" class="com.baidu.entity.Grade" >
<column name="gid" />
</many-to-one>
【dao】
商品对应类型
private TypeBean gtype;
<many-to-one name="gtype" column="tid" class="com.baidu.entity.TypeBean" lazy="false"/>
多对多:学生对课程
student: private Set<Course> cou;
<set name="cou" table="sc" lazy="false">
<key column="sid"></key>
<many-to-many class="com.baidu.entity.Course" column="cid"/>
</set>
多对多: 课程对学生
grade :private Set<Student> stu;
<set name="stu" table="sc" lazy="false">
<key column="cid"/>
<many-to-many class="com.baidu.entity.Student" column="sid"/>
</set>
------解决思路----------------------