[Hibernate3-Summary] [section 2nd]: Session functions of hibernate core applications

Source: Internet
Author: User

# [Differences between opensession and getcurrentsession]

<1> the generated session instance.
The session generated by opensession is an instance of sessionimpl;
Getcurrentsession generates a proxy object $ proxy
<2> New and Old session instances.
Each open opensession is a new session;
Getcurrentsession: the first call returns a new session, and then puts it into the thread context. Each time you get the same session that already exists in the context before the Transaction Completes [commit/rollback.
<3> auto-disable.
The opensession session needs to be closed manually;
The session of getcurrentsession is automatically closed internally when the transaction is completed, and you do not need to close it yourself in the code.
<4> usage conditions.
Use opensession;
Getcurrentsession must be configured with current_session_context_class in the hibernate configuration file; otherwise, usage fails.

<! -- Enable automatic session context management for hibernate --> <property name = "current_session_context_class"> thread </property>

# [Difference between get and load]
<1> delayed loading.
Get does not support delayed loading. It directly sends out SQL queries. Load delays loading and uses SQL queries to query databases only when object data is used.
<2> instance type of the returned object.
Get returns the original type of object instance; load returns the proxy object, $ response sist ..
<3> the object cannot be found.
Get query without data: NULL is returned directly, and nullpointerexception is returned during the call; load query without data: A proxy object is returned, which is the above $ Javassist.. org. hibernate. objectnotfoundexception.

# [Differences between SAVE and persist]
<1> the difference lies mainly outside the transaction.
Their main difference lies in the absence of transactions. If the SQL statement is issued immediately after the save operation is performed outside the transaction, the session contains the persistence Object ID, while persist does not issue the SQL statement. After the execution, the ID obtained immediately is 0, and no ID is returned immediately.

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.