您好,登錄后才能下訂單哦!
Hibernate中merge()方法的作用是什么,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
具體的實現(xiàn)過程如下:
背景:
◆Account 和 Group 兩個對象,設(shè)置了雙向的many-to-many關(guān)系,lazy=true
◆不使用open session in view 模式
◆不使用hibernate二級緩
◆考慮web應(yīng)用場景,設(shè)置account和group關(guān)聯(lián)時,只需要group和account的id就夠了。
◆數(shù)據(jù)庫中存在兩個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設(shè)置了兩個group 輸出結(jié)果:
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());
輸出結(jié)果:
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 (?, ?)
結(jié)論:
1. Hibernate merge()方法會導致執(zhí)行查詢group對象的select語句,在調(diào)用merge()命令時立即執(zhí)行(條件:目標group對象沒有被緩存)
2. 無論Hibernate merge()或save()方法,insert語句都在***執(zhí)行,并非在調(diào)用相應(yīng)命令時立即執(zhí)行
3. 直接調(diào)用merge()方法時,會返回一個新的instance,原po保持不變
4. save()之后,po中的group對象并沒有被關(guān)聯(lián)到session,因此查詢group(id=1)會觸發(fā)select語句
5. save()之后,po對象被關(guān)聯(lián)到session,再次查詢,不會觸發(fā)select語句,并且不會檢查group對象是否被關(guān)聯(lián)到session
6. save()之后再調(diào)用merge,返回的是同一個instance,但其關(guān)聯(lián)group對象會被更新
如果在add一個對象之后,如果存在關(guān)聯(lián)對象,并且需要再同一個hibernate session中進行回顯,則建議使用Hibernate merge()方法。
看完上述內(nèi)容,你們掌握Hibernate中merge()方法的作用是什么的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。