Hibernate中merge()方法的作用是什么
Hibernate中merge()方法的作用是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站制作、龙山网络推广、小程序制作、龙山网络营销、龙山企业策划、龙山品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供龙山建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
具体的实现过程如下:
背景:
◆Account 和 Group 两个对象,设置了双向的many-to-many关系,lazy=true
◆不使用open session in view 模式
◆不使用hibernate二级缓
◆考虑web应用场景,设置account和group关联时,只需要group和account的id就够了。
◆数据库中存在两个group: 1.administrators, 2.engineers而po对象中,group信息为:1.invalid, 2.any one
代码A:
Account account = (Account) getHibernateTemplate().merge(po); Long id = account.getId(); System.out.println("\tGet obj after added in dao start ..."); Account readAccount = (Account) getHibernateTemplate().get( Account.class, id); System.out.println("\tGet obj after added in dao end ..."); System.out.println("\tIs po==readAccount ? " + (po == readAccount)); System.out.println("\tShow detai of po: " + po.toDetailString()); System.out.println("\tShow detai of readAccount: " + readAccount.toDetailString());
其中,为po设置了两个group 输出结果:
Hibernate: select ... from SYS_GROUPS where ID=? Hibernate: select ... from SYS_GROUPS where ID=? Get obj after added in dao start ... Get obj after added in dao end ... Is po==readAccount ? false Show detai of po: Account[0.account_22, groups[2.any one 1.invalid ]] Show detai of readAccount: Account[22.account_22, groups[2.engineers 1.administrators ]] Hibernate: insert into SYS_ACCOUNTS (...) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into SYS_GROUP_MEMBER (ACCOUNT_ID, GROUP_ID) values (?, ?) Hibernate: insert into SYS_GROUP_MEMBER (ACCOUNT_ID, GROUP_ID) values (?, ?)
代码B:
Long id = (Long) getHibernateTemplate().save(po); System.out.println("\tGet obj after added in dao start ..."); Group group = (Group)getHibernateTemplate().get(Group.class,new Long(1)); System.out.println("\tGroup detai:" + group.toString()); Account readAccount = (Account) getHibernateTemplate().get( Account.class, id); System.out.println("\tGet obj after added in dao end ..."); System.out.println("\tIs po==readAccount ? " + (po == readAccount)); System.out.println("\tShow detai of po: " + po.toDetailString()); System.out.println("\tShow detai of readAccount: " + readAccount.toDetailString()); getHibernateTemplate().merge(readAccount); Account readAgain = (Account) getHibernateTemplate().get(Account.class, id); System.out.println("\tIs po==readAgain ? " + (readAgain == po)); System.out.println("\tIs readAgain== readAccount? " + (readAgain == readAccount)); System.out.println("\tShow detai again: " + readAgain.toDetailString());
输出结果:
Get obj after added in dao start ... Hibernate: select ... from SYS_GROUPS where ID=? Group detai:Group 1. administrators Get obj after added in dao end ... Is po==readAccount ? true Show detai of po: Account[27.account_27, groups[1.invalid 2.any one ]] Show detai of readAccount: Account[27.account_27, groups[1.invalid 2.any one ]] Hibernate: select ... from SYS_GROUPS where ID=? Is po==readAgain ? true Is readAgain== readAccount? true Show detai again: Account[27.account_27, groups[1.administrators 2.engineers ]] Hibernate: insert into SYS_ACCOUNTS (...) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into SYS_GROUP_MEMBER (ACCOUNT_ID, GROUP_ID) values (?, ?) Hibernate: insert into SYS_GROUP_MEMBER (ACCOUNT_ID, GROUP_ID) values (?, ?)
结论:
1. Hibernate merge()方法会导致执行查询group对象的select语句,在调用merge()命令时立即执行(条件:目标group对象没有被缓存)
2. 无论Hibernate merge()或save()方法,insert语句都在***执行,并非在调用相应命令时立即执行
3. 直接调用merge()方法时,会返回一个新的instance,原po保持不变
4. save()之后,po中的group对象并没有被关联到session,因此查询group(id=1)会触发select语句
5. save()之后,po对象被关联到session,再次查询,不会触发select语句,并且不会检查group对象是否被关联到session
6. save()之后再调用merge,返回的是同一个instance,但其关联group对象会被更新
如果在add一个对象之后,如果存在关联对象,并且需要再同一个hibernate session中进行回显,则建议使用Hibernate merge()方法。
看完上述内容,你们掌握Hibernate中merge()方法的作用是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
当前标题:Hibernate中merge()方法的作用是什么
转载注明:http://myzitong.com/article/peeogg.html