get() method

If you want to get an object by its ID (or Primary Key), then Hibernate has three methods for this:

  • load()
  • get()
  • find()

They do the same thing, but there are nuances. The general format of the get() method is:

EntityClass Name = session.get(EntityClass.class, Object primaryKey);

The get() method takes as its second parameter the ID (Primary Key) of the object it needs to return. It then loads that object from the database and returns it. Example:

User user = session.get(User.class, 2);

If a record with this ID is not found in the database, then the method will return null.

load() method

The second method for loading an object is the load() method . The general format of the load() method is the same:

EntityClass Name = session.load(EntityClass.class, Object primaryKey);

However, its behavior is different from the get() method .

First, this method does not return a real object, but a proxy: a virtual stub.

Secondly, when using the load() method , there is no check whether there is such an entry in the database. Instead, Hibernate immediately creates a proxy object with the passed ID and returns it.

Thirdly, all work with the database will occur when calling the methods of the proxy object. If you try to call, for example, the getName() method , then the first call to the database will occur. Example:

User user = session.load(User.class, new Integer(123));
String name = user.getName(); //this is where the first call to the database will occur

The load() method should not be used to check for the presence of objects in the database - it simply will not show this. In addition, if you pass an invalid ID, such as null, to it, it will simply return null.

find() method

The find() method was passed on to the Session interface from the JPA standard. And as you know, this standard describes not just the signature of methods, but also regulates the behavior.

This method works exactly like the get() method . If the object was not found by the passed key, then the method will simply return null.

User user = session.find(User.class, -2); //method will return null

refresh() method

Another useful method that has to do with loading an object from a database is the refresh() method .

Remember the persist() method that updated the data in the database based on the passed object? So, the refresh() method works exactly the opposite: it updates an existing object based on data from the database.

This behavior is necessary if, for example, when writing an object to the database, various stored procedures are called there that correct the written data.

In such cases, it can be useful to reread the object from the database if there is a chance that it has changed. Example:

User user = new User();
user.setName("Kolyan");
session.persist(user);
session.flush();  //Force called SQL INSERT and call triggers

session.refresh(user);
// here we continue to work with the updated object
undefined
1
Task
Module 4. Working with databases, level 11, lesson 3
Locked
task1103
task1103