Method can return an instance of a proxy class that does not have the Entity data loaded, and the Get method always returns an object with Entity data. (For load and get method return types: "Get methods always return entity classes" is not actually correct, if the Get method finds the object corresponding to the ID in the session cache, if the object is previously proxied, if it was used by the Load method, or delayed by other associated objects, then the original proxy object is returned, not the entity class object, if the proxy object has not loaded the Entity data (that is, other than the ID of the property data), then it will query the level two cache or the database to load the data, but the return is the proxy object, Only the Entity data has been loaded. )
In short, for get and load the fundamental difference, in a word, hibernate for the load method that the data in the database must exist, can be assured that the use of agents to delay loading, if the use of the problem found in the process, can only throw exceptions, and for the Get method, Hibernate must obtain the actual data, otherwise null is returned.
Finally, let's analyze why there are so many different articles on the Internet that are less accurate! The first may be the version problem, hibernate version is different, the operating mechanism is not the same, followed by a lot of friends just to share their experience with people, and not through the full range of code detection, and finally some technical cattle, expression more casual. So I hope that when you learn the knowledge of the time do not blindly follow others superstition Authority, must be comprehensive multi-data, comparison and collation, and then through their own practice test, so that the knowledge is true and effective.
Second article
- Users user = (users) session.load (Users.class, userId);
Get Loading method:
Java code
- Users user = (users) session.get (Users.class, userId);
Two Load method differences:
Difference 1: If the database has no userid in the object. If it is loaded by the Get method, a null is returned, and if loaded by load, a proxy object is returned if the subsequent code calls a property of the user object (such as User.getpassword () ) throws an exception: Org.hibernate.ObjectNotFoundException;
The difference 2:load supports lazy loading, and get does not support lazy loading.
Other words:
Java code
- Users user = (users) session.load (Users.class, userId);
This code does not execute the database query, but only when the user is used to execute the database query.
and
Java code
- Users user = (users) session.get (Users.class, userId);
Execute the database query immediately. So users user = (users) session.load (Users.class, userId); no SQL is executed.
Attention:
Java code
- Users user = (users) session.load (Users.class, userId);
- System.out.println (User.getid ());
The above 2 lines of code do not perform database operations. Since load will store a map object in the Hibernate cache, the map key is the value of the UserID, but when you GetID (), it goes to the first-level cache and takes the key value of the map instead of executing the database query. So I won't report anything wrong. Does not perform any database operations.
Difference: It is more obvious that there are no printed statements
Hibernate Get and load differences