2.1 Introduction to the Query classes

By the way, another important point is the Query helper class. You could see it in this example:

public List<Employee> getAllEmployes() {
    try (Session session = sessionFactory.openSession()) {
            Query<Employee> query = session.createQuery("from Employee", Employee.class);
            return query.list();
    }
}

In fact, Query is an interface and it has several implementations for different cases. But for simplicity, I will continue to call it a class. This is, let's say, a class in a broad sense - in terms of OOP.

Note. There used to be two classes:

  • Query to describe the query.
  • TypedQuery to describe a query with a known type.

The first appeared when Hibernate already existed, and there were no generics yet. Then, after the release of JDK 5, another class was added to Hibernate - TypedQuery, which already supported the typing of the query result.

But, as far as I remember, starting with the 5th version of Hibernate, only one typed class was left, and it is now called Query.

The standard way to create a Query is:


Query<Employee> query = session.createQuery("from Employee", Employee.class);

You learned how to create Query objects, but how do you execute these queries?

It's even simpler here - we just call the list() method on the Query object:


Query<Employee> query = session.createQuery("from Employee", Employee.class);
List<Employee> resultLіst = query.list();

The list() method has a JPA synonym, a method that does the same thing but is called getResultList() . You can sometimes see it in code written by other programmers.

By the way, if the query implies that the result will be in a single result, then it is easier to use the uniqueResult() method to call the query .


Query<Employee> query = session.createQuery("from Employee where id = 1", Employee.class);
Employee result = query.uniqueResult();

The uniqueResult() method has a JPA synonym, the singleResult() method . It was introduced for Hibernate's compatibility with the JPA standard. He does exactly the same thing.

2.2 Query class methods

In fact, the Query class has a lot of different methods. Below I will talk about three more of them.

The first is the stream() method . And its JPA synonym getResultStream() .

Both of these methods return a stream of data instead of a list. This approach can be very efficient when you don't need all the objects obtained as a result of a query at once. Or there is a possibility that only the first of them will be used.

Example:


Query<Employee> query = session.createQuery("from Employee where id > 100", Employee.class);
Stream<Employee> stream = query.stream();

The second method is the executeUpdate() method . You can write a query that will change something in the database. In this case, it is necessary that Hibernate does not use a read-only transaction when accessing the database.

Request example: we decided to raise the level of all users by 1.


Query<User> query = session.createQuery("update User set level=level+1", User.class);
int count = query.executeUpdate();

The executeUpdate() method will return the number of rows that have actually been modified.

And finally the third method is scroll() . We will tell you a little more about it.

2.3 Scroll class methods

This method is somewhat similar to the stream() method . Only it allows you to move through the list of results without pulling out the results at all. That is, you can execute a query, then scroll it to the millionth line of the result and start reading data from there.

Such an advanced iterator.


Query<Employee> query = session.createQuery("from Employee where id > 100", Employee.class);
ScrollableResults<Employee> scroll = query.scroll();

The ScrollableResults object has the following methods:

Method Description
R get() Returns the current element
next() Moves the pointer to the next element
previous() Moves the pointer to the previous element
scroll(int size) Scroll forward by size lines
position(int pos) Makes element pos number the current element
last() The current element is now the last
first() The current element is now the first
getRowNumber() Returns the current line number
setRowNumber() Sets the current line number

Let's say you ran a query and you want to get the last element. Here's how to do it:


Query<Employee> query = session.createQuery("from Employee where id > 100", Employee.class);
ScrollableResults<Employee> scroll = query.scroll();
scroll.last();
Employee lastEmployee = scroll.get();
undefined
1
Task
Module 4. Working with databases, level 10, lesson 1
Locked
task1002
task1002
undefined
1
Task
Module 4. Working with databases, level 10, lesson 1
Locked
task1003
task1003