1.在Classroom.java增加Set<Student> stus:
public class Classroom {
private int id;
private String name;
private int grade;
private Set<Student> stus;
public Set<Student> getStus() {
return stus;
}
public void setStus(Set<Student> stus) {
this.stus = stus;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
2.Classroom.hbm.xml:
<hibernate-mapping>
<class name="model.Classroom" table="CLASSROOM">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="grade" type="int">
<column name="GRADE" />
</property>
<set name="stus" lazy="extra" inverse="true">
<key>
<column name="c_id" />
</key>
<one-to-many class="model.Student" />
</set>
</class>
</hibernate-mapping>
注意:(1).lazy=“extra”是取size的时候智能化,inverse="true"是不在ClassRoom这一端维护,key是在对方(Student)的外键名称,class是集合中元素的类型
test01:
void test3() {
Session s=null;
try {
s=HibernateUtil.getSession();
s.beginTransaction();
Student stu1=new Student();
stu1.setName("a");
stu1.setNumber(1);
s.save(stu1);
Student stu2=new Student();
stu2.setName("b");
stu2.setNumber(2);
s.save(stu2);
Classroom cla=new Classroom();
cla.setGrade(2017);
cla.setName("计算机");
Set<Student> stus=new HashSet<>();
stus.add(stu1);
stus.add(stu2);
cla.setStus(stus);
s.save(cla);
s.getTransaction().commit();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
s.getTransaction().rollback();
}finally {
HibernateUtil.closeSession(s);
}
}
注意:(1).5条sql,3条save,2条update,cla.setStus(stus);实际上是在stu端设置c_id,由于2个stu,所以设置了2个stu,就多发2条update stu的语句
test02:
void test4() {
Session s=null;
try {
s=HibernateUtil.getSession();
s.beginTransaction();
Classroom cla=new Classroom();
cla.setGrade(2017);
cla.setName("计算机");
s.save(cla);
Student stu1=new Student();
stu1.setName("a");
stu1.setNumber(1);
stu1.setCla(cla);
s.save(stu1);
Student stu2=new Student();
stu2.setName("b");
stu2.setNumber(2);
stu2.setCla(cla);
s.save(stu2);
s.getTransaction().commit();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
s.getTransaction().rollback();
}finally {
HibernateUtil.closeSession(s);
}
}
注意:(1).先设置1再设置多,即发3条sql语句
test03:
void test5() {
Session s=null;
try {
s=HibernateUtil.getSession();
s.beginTransaction();
Classroom cla=s.load(Classroom.class, 3);
System.out.println(cla.getStus().size());
Student stu=s.load(Student.class, 6);
System.out.println(stu.getCla().getGrade());
s.getTransaction().commit();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
s.getTransaction().rollback();
}finally {
HibernateUtil.closeSession(s);
}
}
注意:(1).load classroom一条语句,把classroom存在缓存中,然后取size用count再发一条语句,然后取load stu再发一条语句,然后取stu.getCla()不用再发取classroom的语句了,因为缓存中有。
test04:
void test6() {
Session s=null;
try {
s=HibernateUtil.getSession();
s.beginTransaction();
Student stu=s.load(Student.class, 6);
System.out.println(stu.getCla().getGrade());
s.getTransaction().commit();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
s.getTransaction().rollback();
}finally {
HibernateUtil.closeSession(s);
}
}
注意:(1).load stu一条语句,stu.getCla再发一条取cla的语句,因为缓存没有cla,所以总共2条语句