Load,get; Find,iterator; The difference between merge,saveorupdate,lock

Source: Internet
Author: User

First, Load,get
(1) When the record does not exist, the Get method returns the Null,load method to produce an exception

(2) The Load method can return the entity's proxy class, and the Get method returns the real entity class

(3) The Load method can take full advantage of Hibernate's internal cache and the existing data in the level two cache, while the Get method only makes data lookups in the internal cache, and if no data is found it will go over the level two cache and call the SQL query database directly.
    (4)   Perhaps others have modified the data in the database, how load finds the data in the cache, no longer accesses the database, and get returns the most recent data.
 
Second, Find,iterator
      (1) Iterator first gets the ID of the qualifying record, and then finds the data in the local cache with the ID. Found in the database again, the result is in the cache again. N+1 SQL.
(2) Find generates SQL statements directly to the database, the data found in the cache, a SQL.

Three, hibernate-generated DAO class function function description (merge,saveorupdate,lock)

/**
* Copy the properties of the object that passed in the detached state to the persisted object and return the persisted object.
* If there are no persisted objects associated with the session, load one.
* If the incoming object is not saved, a copy is saved and returned as a persistent object, and the incoming object remains in the detached state.
*/
Public sysuser Merge (Sysuser detachedinstance) {
Log.debug ("Merging Sysuser instance");
try {
Sysuser result = (Sysuser) gethibernatetemplate (). Merge (
Detachedinstance);
Log.debug ("merge successful");
return result;
} catch (RuntimeException re) {
Log.error ("Merge Failed", re);
throw re;
}
}

/**
* Persist the incoming object and save it. If the object is not saved (transient state), the Save method is called.
* If the object is saved (detached state), call the Update method to re-associate the object with the session.
*/
public void Attachdirty (Sysuser instance) {
Log.debug ("Attaching dirty Sysuser instance");
try {
Gethibernatetemplate (). Saveorupdate (instance);
Log.debug ("Attach successful");
} catch (RuntimeException re) {
Log.error ("Attach failed", re);
throw re;
}
}

/**
* Set the incoming object state to the transient state
*/
public void Attachclean (Sysuser instance) {
Log.debug ("Attaching clean Sysuser instance");
try {
Gethibernatetemplate (). Lock (instance, lockmode.none);
Log.debug ("Attach successful");
} catch (RuntimeException re) {
Log.error ("Attach failed", re);
throw re;
}
}

Load,get; Find,iterator; The difference between merge,saveorupdate,lock

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.