Hibernate learning notes Merge () method-add object

Source: Internet
Author: User

Merge

Add operation

Background:
Two objects, account and group, are set with a two-way relationship between allow-to-minus, lazy = true
Do not use open session in view mode
Do not use hibernate second-level cache

Considering the web application scenario, you only need the ID of the group and account when setting the association between accounts and groups.
There are two groups in the database: 1. administrators, 2. Engineers
In the Po object, the group information is: 1. invalid, 2.any one

Code:
Account account = (account) gethibernatetemplate (). merge (PO); <br/> long id = account. GETID (); <br/> system. out. println ("/tget OBJ after added in Dao start... "); <br/> account readaccount = (account) gethibernatetemplate (). get (<br/> account. class, ID); <br/> system. out. println ("/tget OBJ after added in Dao end... "); <br/> system. out. println ("/tis po = readaccount? "+ (Po = readaccount); <br/> system. out. println ("/tshow detai of Po:" + Po. todetailstring (); <br/> system. out. println ("/tshow detai of readaccount:" + readaccount. todetailstring (); <br/>

Two groups are set for the Po.

Output result:
Hibernate: Select... from sys_groups where id =? <Br/> hibernate: Select... from sys_groups where id =? <Br/> Get OBJ after added in Dao start... <br/> Get OBJ after added in Dao end... <br/> is po = readaccount? False <br/> show detai of Po: account [0. account_22, groups [2.any one 1. invalid] <br/> show detai of readaccount: account [22. account_22, groups [2. engineers 1. administrators] <br/> hibernate: insert into sys_accounts (...) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) <Br/> hibernate: insert into sys_group_member (account_id, group_id) values (?, ?) <Br/> hibernate: insert into sys_group_member (account_id, group_id) values (?, ?) <Br/>

 

Code B:

Long id = (long) gethibernatetemplate (). save (PO); <br/> system. out. println ("/tget OBJ after added in Dao start... "); <br/> group Group = (Group) gethibernatetemplate (). get (group. class, new long (1); <br/> system. out. println ("/tgroup detai:" + group. tostring (); <br/> account readaccount = (account) gethibernatetemplate (). get (<br/> account. class, ID); <br/> system. out. println ("/tget OBJ after added in Dao End... "); <br/> system. Out. println ("/tis po = readaccount? "+ (Po = readaccount); <br/> system. out. println ("/tshow detai of Po:" + Po. todetailstring (); <br/> system. out. println ("/tshow detai of readaccount:" <br/> + readaccount. todetailstring (); </P> <p> gethibernatetemplate (). merge (readaccount); <br/> account readagain = (account) gethibernatetemplate (). get (account. class, <br/> ID); <br/> system. out. println ("/tis po = readagain? "+ (Readagain = po); <br/> system. Out. println ("/tis readagain = readaccount? "<Br/> + (readagain = readaccount); <br/> system. out. println ("/tshow detai again:" + readagain. todetailstring (); <br/>

Output result:
Get OBJ after added in Dao start... <br/> hibernate: Select... from sys_groups where id =? <Br/> group detai: group 1. administrators <br/> Get OBJ after added in Dao end... <br/> is po = readaccount? True <br/> show detai of Po: account [27. account_27, groups [1. invalid 2.any one] <br/> show detai of readaccount: account [27. account_27, groups [1. invalid 2.any one] <br/> hibernate: select... from sys_groups where id =? <Br/> is po = readagain? True <br/> is readagain = readaccount? True <br/> show detai again: account [27. account_27, groups [1. administrators 2. engineers] <br/> hibernate: insert into sys_accounts (...) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) <Br/> hibernate: insert into sys_group_member (account_id, group_id) values (?, ?) <Br/> hibernate: insert into sys_group_member (account_id, group_id) values (?, ?) <Br/>

Conclusion:

1. the Merge () method will cause the SELECT statement of the query group object to be executed immediately when the Merge () command is called (condition: the target group object is not cached)

2. No matter the merger () or save () method, the insert statement is executed at the end, not immediately when the corresponding command is called.

3. When the Merge () method is called directly, a new instance is returned, and the original Po remains unchanged.

4. After saving (), the Group object in the Po is not associated with the session. Therefore, querying group (ID = 1) triggers the SELECT statement.

5. After saving (), the PO object is associated with the session and queried again. The SELECT statement is not triggered and the group object is not checked for being associated with the session.

6. After saving (), call merge and return the same instance, but the associated group object will be updated.

If an associated object exists after an object is added and needs to be echo in the same hibernate session, we recommend that you use the Merge () method.

Refer:
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.