标签:hibernate
public static void main(String[] args) { // test(); // test1(); // test2(); // test3(); // test4(); // test5(); // 测试one2many 单向 test6(); // 测试单向 维护关系 delete删除操作 // test7(); // 测试单向 维护关系 delete更新操作 // test8(); } private static void test8() { // 获得 session 开启事务 SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); // 新建 一方数据 设置ID 为主键值 PersonInfo personinfo = new PersonInfo(); personinfo.setPerId("4028e5734cf9ddc8014cf9ddc9860001"); // 删除该对象 并未执行删除多方数据 session.delete(personinfo); session.flush(); // 提交事务 session.getTransaction().commit(); // 关闭session session.close(); } private static void test7() { // 获得 session 开启事务 SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); // 载入该对象 PersonInfo personinfo = (PersonInfo) session.load(PersonInfo.class, "4028e5734cf9d163014cf9d164fc0001"); // 取出多方数据集合 Set<One2Many> set = personinfo.getOne2many(); Iterator<One2Many> iterator = set.iterator(); // 迭代输出 多方 主键值 while (iterator.hasNext()) { System.out.println(iterator.next().getTest4()); } // 删除一方数据 session.delete(personinfo); // 提交事务 session.getTransaction().commit(); // 关闭session session.close(); } private static void test6() { // 获得 session 开启事务 SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); // 创建一方 对象值 PersonInfo personinfo = new PersonInfo(); personinfo.setPerName("单向one2many"); // 创建多方数据并存入集合 One2Many o = new One2Many(); o.setTest2("单向1"); One2Many o2 = new One2Many(); o2.setTest2("单向2"); One2Many o3 = new One2Many(); o3.setTest2("单向3"); HashSet<One2Many> set = new HashSet<One2Many>(); set.add(o); set.add(o2); set.add(o3); // 将数据绑定一方数据 personinfo.setOne2many(set); // 保存一方数据 session.save(personinfo); // 提交事务 session.getTransaction().commit(); // 关闭session session.close(); }
方法7与方法8的 执行结果是不一样的。
方法8 运行结果:
Hibernate: update Test set b=null where b=?
Hibernate: update one2many set test3=null where test3=?
Hibernate: delete from personinfo where perId=?
执行的是更新外键值为null
方法7执行结果:
Hibernate: select personinfo0_.perId as perId0_2_, personinfo0_.perName as perName0_2_, personinfo0_.perSex as perSex0_2_, personinfo0_.perAge as perAge0_2_, personinfo0_.perPhone as perPhone0_2_, personinfo0_.PerAddress as PerAddress0_2_, personinfo0_.perJob as perJob0_2_, personinfo0_.perEntryTime as perEntry8_0_2_, personinfo0_.PerDeparturetime as PerDepar9_0_2_, personinfo0_.perCreateDate as perCrea10_0_2_, personinfo0_.perLastModifyDate as perLast11_0_2_, mtwo2many1_.e as e4_, mtwo2many1_.a as a4_, mtwo2many1_.a as a4_0_, mtwo2many1_.b as b4_0_, mtwo2many1_.c as c4_0_, mtwo2many1_.d as d4_0_, mtwo2many1_.e as e4_0_, one2many2_.test3 as test4_5_, one2many2_.test4 as test1_5_, one2many2_.test4 as test1_6_1_, one2many2_.test1 as test2_6_1_, one2many2_.test2 as test3_6_1_, one2many2_.test3 as test4_6_1_ from personinfo personinfo0_ left outer join Test2 mtwo2many1_ on personinfo0_.perId=mtwo2many1_.e left outer join one2many one2many2_ on personinfo0_.perId=one2many2_.test3 where personinfo0_.perId=?
Hibernate: select one2one0_.test4 as test1_1_0_, one2one0_.test1 as test2_1_0_, one2one0_.test2 as test3_1_0_, one2one0_.test3 as test4_1_0_ from one2one one2one0_ where one2one0_.test4=?
4028e5734cf9e355014cf9e356720003
4028e5734cf9e355014cf9e356720004
4028e5734cf9e355014cf9e356720002
Hibernate: select mone2many0_.b as b1_, mone2many0_.a as a1_, mone2many0_.a as a3_0_, mone2many0_.b as b3_0_, mone2many0_.c as c3_0_ from Test mone2many0_ where mone2many0_.b=?
Hibernate: update one2many set test3=null where test3=?
Hibernate: delete from one2many where test4=?
Hibernate: delete from one2many where test4=?
Hibernate: delete from one2many where test4=?
Hibernate: delete from personinfo where perId=?
执行delete 是删除操作
标签:hibernate
原文地址:http://yjm199.blog.51cto.com/4408395/1639287