代码
public class Main {
public static void main(String[] args) {
SessionFactory sty = HibernateUtil.getSessionFactory();
Session se=sty.openSession();
Transaction tr = se.beginTransaction();
ClassInfo cla = new ClassInfo();
Student stu = new Student();
Student stu1 = new Student();
cla.setCid("101");
cla.setCmc("321");
cla.setCkh("哈哈");//班级口号
stu.setSid("2");
stu.setSname("李红慢");
stu.setAge(18);
stu1.setSid("3");
stu1.setSname("吴");
stu1.setAge(18);
Set set = new HashSet();
set.add(stu);
set.add(stu1);
//Student 关联 Class
cla.setStu(set);
//保存
se.save(cla);
se.save(stu);
se.save(stu1);
tr.commit();
se.close();
}
}
映射文件
运行结果