Junior — Middle
Explain what is meant by Query in the context of Hibernate and how it is used to retrieve data.
sobes.tech AI
Answer from AI
In Hibernate Query — it is an object that represents a database query written in HQL (Hibernate Query Language) or SQL. Using Query, you can retrieve data from the database by specifying selection conditions.
Example of using Query to get a list of users with a specific name:
String hql = "FROM User WHERE name = :userName";
Query<User> query = session.createQuery(hql, User.class);
query.setParameter("userName", "Ivan");
List<User> users = query.list();
Here, a query is created with a parameter, and data is fetched from the table associated with the User entity. Query allows flexible query formation and retrieving results as Java objects.