Java.Hibernate.Beginner.Do i need write select word or not ?

No — you don’t have to use SELECT in HQL if you want to fetch entire entities. In HQL, you can simply write:

FROM User

This is equivalent to:

SELECT u FROM User u

Hibernate understands both forms — when you omit SELECT, Hibernate implicitly selects the entire entity.

🔹 Examples:

Without SELECT:

String hql = "FROM User";
List<User> users = session.createQuery(hql, User.class).list();

With SELECT:

String hql = "SELECT u FROM User u";
List<User> users = session.createQuery(hql, User.class).list();

🔹 When do you need SELECT?
You must use SELECT if you:
✅ Want to retrieve specific properties (projections), not entire entities:

String hql = "SELECT u.username FROM User u";
List<String> usernames = session.createQuery(hql, String.class).list();

✅ Want to use aggregate functions like COUNT(), SUM(), etc.:

String hql = "SELECT COUNT(u) FROM User u";
Long count = session.createQuery(hql, Long.class).uniqueResult();

Key takeaway:
For entire entities, you can skip SELECT and just write FROM Entity.
For specific fields or aggregates, you must use SELECT.

This entry was posted in Без рубрики. Bookmark the permalink.