Hibernate learning Note Merge () method--add Object

Source: Internet
Author: User

Merge

Add action


Background:
Account and Group two objects, set up a two-way many-to-many relationship, Lazy=true
Do not use open session in view mode
Do not use Hibernate level two cache

When you set account and group associations, you need only the IDs of the group and the account to consider the Web application scenario.
There are two group:1.administrators in the database, 2.engineers
And the Po object, the group information is: 1.invalid, 2.any one


Code 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 on 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 ());

Where two group is set for the PO

Output results:
Hibernate:select. From sys_groups where id=? Hibernate:select. From sys_groups where id=? Get obj after added in DAO start ... Get obj on 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_ 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 (?,?)

Code 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 on 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 ());


Output results:
Get obj after added in DAO start ... Hibernate:select. From sys_groups where id=? Group Detai:group 1. Administrators get obj after added on 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_ 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 (?,?)


Conclusion:

1. The merge () method results in the execution of a SELECT statement that queries the group object and executes immediately when the merge () command is invoked (condition: The target group object is not cached)

2. Regardless of the merger () or Save () method, the INSERT statement is executed at the end, not immediately when the corresponding command is invoked

3. When you call the merge () method directly, a new instance is returned and the original PO remains unchanged

4. After save (), the Group object in the PO is not associated with the session, so query group (id=1) triggers the SELECT statement

5. After save (), the PO object is associated with the session, queries again, does not trigger the SELECT statement, and does not check whether the group object is associated with the session

6. Save () and then call the merge, return the same instance, but its associated group object will be updated

The merge () method is recommended if, after an object is add, if there is an associated object and you need to echo back in the same hibernate session.

Reference:
Hibernate Session.merge () Javadoc
Open session in view mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.