Methods In session

Source: Internet
Author: User

Session

Manage the task units of a database, that is, managing the addition, deletion, modification, and query operations in the database and committing transactions.

Method crud: Save (), delete (), load (), get (), update (), saveorupdate (), clear (). Flush ()

Session. begintransaction ();

Session. Save (Object OBJ );

Session. gettransaction (). Commit ();

Session. Close ();

Get ()AndLoad ()Difference

L when searching, it will give priority to the session cache.

L when the corresponding record does not exist in the search, the load method does not return an error when it is not found, and the get method returns an error when it is not found.

L load returns a proxy object. When the object content is actually used, an SQL statement is initiated. Get directly initiates an SQL statement to retrieve it from the database without delay.

Update ()Method

1 is used to update the detached object. After the update is complete, the object becomes persistent.

2. An error will be reported when you update the transient object. You can update the transient object of your Set ID (provided that the ID exists in the Database.

3. The persistence object will be updated as long as different fields are set.

4. Update partially changed fields (three methods)

L XML sets the property tag update = true | false attribute,

Annotation sets the @ column (updatable = false) attribute. This method is less useful and less flexible than Z.

L set the dynamic-update = "true" attribute of the class label in XML,

Yes in the same session, but not across sessions. the cross-session implementation method can use the Merge () of the session (). the merge method first loads data from the database, compares the obtained data with those in the database, and then updates the changed fields.

Jpa1.0 annotation does not have corresponding attributes, Hibernate

L use hql (ejbql) (recommended)

Clear ()Method:

The load () and get () methods give priority to the session cache. Calling the clear () method will forcibly clear the session cache and will not deal with the database.

Flush ()Method:

The flush () method forces synchronization from memory (session cache) to database. Synchronization is performed only when the session transaction is committed by default.

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.