A detailed comparison of the Session.get () method and the load () method in Hibernate

Source: Internet
Author: User

A simple understanding of the Get method and the Load method

(1) The Get () method returns the entity class directly, and returns NULL if no data is found. Load () Returns an entity proxy object (currently this object can be automatically converted to an entity object), but when the proxy object is called, a Org.hibernate.ObjectNotFoundException exception is thrown if no data is present

(2) Load first to cache (session cache/Level two cache) to check, if not return a proxy object (not immediately to the DB to find), and so on later use this proxy object operation, only to the DB query, this is what we often say Load supports lazy loading by default (lazy)

(3) Get first to cache (session cache/Level two cache) to check, if not on the DB to check (that is, the issue of SQL immediately). In short, if you are sure that the object is in db with load (), you are not sure to use Get () (This is high efficiency)

(4) Get and load query database

1. If the query is not data, get will return NULL, but will not error, load if the query is not data, then error objectnotfoundexception

2. Use get to query data, (first level/Level two) will immediately issue a query request to the DB (select ...), if you are using Load query data (first level, two level)) even if the query to the object, return a proxy object, if not later use the query results, It does not really send a select to the database, when the programmer uses the results of the query to really emit a select, this phenomenon we call lazy loading (lazy)

3. By modifying the configuration file (*.hbm.xml file), we can cancel lazy loading

[HTML] view Plaincopy

    1. Second, the combination of cache technology to compare get and load differences

      Note: First-level cache is mandatory, level two cache is optional (the default is two-level cache when explained here)

      1.get when querying the first level cache (session cache) to find, no words into the level two cache (between the memory and the hard disk) if the level two cache has not
      Then directly into the database query! Query to and put the results of the query into level two cache! No query, no error
      If you query the same data, get will appear in the first query when the query database but the second time will be directly along the level cache, level two cache lookup can be found!

      2.load in the query is also first-level cache lookup, if found will not query the database, if not found, then to level two cache lookup, if there is no data found in level two cache, the Get method will be based on whether to immediately take advantage of the results of the query, if not used
      The lookup is stopped, and if you want to use it immediately, it will be found in the database, and the result of the query will be put into level two cache! If you do not find the query will be an error!

      Third, in-depth differences between the Get method and the Load method

      (1) The fundamental difference between the Get method and the Load method in Hibernate is:

      1. If you use the Load method, Hibernate believes that the object (database record) corresponding to the ID must exist in the database, so it can be used with confidence, and it can safely use the proxy to delay loading the object. The database is queried for other property data in the object, but in case the record does not exist in the database, there is no way to throw an exception, and the Load method throw exception means that when the data for that object is used, the exception is thrown when the data in the database is not present, rather than when the object is created.

      2. Because the cache in the session is a fairly inexpensive resource for Hibernate, the session cache is checked at load to see if the object exists for that ID, and the proxy is created if it does not exist. So if you know that the ID must have a corresponding record in the database, you can use the Load method to implement lazy loading.
      3. For the Get method, hibernate confirms that the data exists for the ID, first in the session cache, then in the level two cache, and not in the database, and returns null in the database.

      4.get method if the object corresponding to the ID is found in the session cache, the original proxy object, rather than the entity class object, is returned if the object is previously proxied, such as was used by the Load method, or delayed by other associated objects. If the proxy object has not yet loaded the Entity data (that is, the attribute data other than the ID), it queries the level two cache or the database to load the data, but returns the proxy object, except that the entity data has been loaded.
      The 5.get method first queries the session cache, does not query the level two cache, and finally queries the database; Instead, the Load method first queries the session cache, does not create the proxy, and queries the level two cache and database when the data is actually used.

      (2) In short, for get and load the fundamental difference, in a word, hibernate for the load method that the data must exist in the database, can be assured that the use of agents to delay loading, if the use of problems found in the process can only throw exceptions, and for the Get method, Hibernate must obtain the actual data, otherwise null is returned.

      Iv. use of the load and get methods

      ?
      123456789101112131415161718192021222324252627282930313233343536373839 /*     * 比较load方法和get方法的区别     */    public static void CompareLoadAndGet(){        Configuration configuration = new Configuration().configure();        SessionFactory sessionFactory = configuration.buildSessionFactory();        Session session = sessionFactory.openSession();        Transaction ts = null;        Transaction ts1 = null;        try {            /*             * 使用load方法             */            ts = session.beginTransaction();            Employee emp= (Employee) session.load(Employee.class, 9);            System.out.println(load方法获取的员工姓名:+emp.getName());            ts.commit();                        /*             * 使用get方法             */            ts1 = session.beginTransaction();            Employee emp1= (Employee) session.get(Employee.class, 9);            System.out.println(get方法获取的员工姓名:+emp1.getName());            ts1.commit();                    } //      catch (Exception e) {//          if(ts!=null){//              ts.rollback();//          }//      }        finally{            if(session!=null&&session.isOpen()){                session.close();            }        }            }

A detailed comparison of the Session.get () method and the load () method in Hibernate (GO)

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.