In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Get executes the sql statement immediately, while load executes the sql statement only when the non-id attribute of the object is accessed.
2. When get gets an object, if it cannot find it, it returns null. When load gets the object, if it cannot find it, it throws an ObjectNotFoundException.
3. The object obtained by get is a real type object, and the object obtained by load is a proxy object. The class of the proxy object is a class created by Hibernate itself, which is a subclass of the real class!
How to disable delayed loading of load:
a. Add a final modifier to the real User class.
b. Add a lazy= "false" to the element of User.hbm.xml
Execution time of sql in hibernate:
1. When an object goes from no OID (Object id) to OID, the sql statement is executed immediately.
two。 When an object changes from OID (Object id) to OID, the sql statement is executed only when the transaction commits.
3. Load is class-level deferred loading. When we use load to load an object in the database, if the object has never been used, hibernate does not check the library at all!
The sql statement is executed only if the non-id property of the object is accessed!
Add:
The fundamental difference between the get method and the load method in hibernate is that if you use the load method, hibernate believes that the corresponding object (database record) of the id must exist in the database, so it can rest assured to use it, it can rest assured to use a proxy to delay loading the object. The database is queried only when other attribute data in the object is used, but if the record does not exist in the database, there is no way but to throw an exception. The load method throws an exception when the object's data is used, not when the object is created. Since caching in session is a fairly cheap resource for hibernate, the session cache is checked in load to see if the corresponding object of the id exists, and a 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 achieve delayed loading.
For the get method, hibernate will confirm whether the data corresponding to the id exists, first look it up in the session cache, then look in the secondary cache, check the database before it is done, and return null if it is not in the database.
For the return types of load and get methods: although it is said in many books that "get () always returns only entity classes", this is actually not true. If the get method finds the corresponding object of the id in the session cache, if the object happens to be proxied before, such as it has been used by the load method, or delayed loading by other associated objects, then the original proxy object will be returned. Instead of an entity class object, if the proxy object has not yet loaded entity data (that is, attribute data other than id), it will query the secondary cache or database to load the data, but the proxy object will be returned, but the entity data has already been loaded.
The get method first queries the session cache, if not, the secondary cache, and finally the database; on the contrary, when the load method is created, it first queries the session cache, creates a proxy, and queries the secondary cache and database only when the data is actually used.
In short, for the fundamental difference between get and load, in a word, hibernate believes that the data must exist in the database for the load method, so you can rest assured to use a proxy to delay loading, and if you find a problem in the process of use, you can only throw an exception; for the get method, hibernate must get the real data, otherwise it will return null.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.