Basically, if you generate queries, you will probably get to the database, an exception to this is if you cached the query and parameters.
Hibernation requests (whether you use criteria or HQL) will only return objects from the session cache (first level cache) if you get it using @Id.
To cache a request, you can use the following syntax:
session.createQuery("from X as x").setCacheable(true);
Edited for comments:
The request does not match the get request with @Id. To get an object by its @Id, you should write something like:
Entity myEntity = sessionFactory.getCurrentSession().get(Entity.class, 1);
source share