✅ Short Answer
Both HQL and JPQL are basically the same thing (JPQL is the standard; HQL is Hibernate’s dialect).
Hibernate will add the @Where
clause automatically if you query the entity directly — whether you use:
FROM User u
(HQL syntax), orSELECT u FROM User u
(JPQL/HQL syntax).
✅ So in both these cases, Hibernate does include the @Where
filter from @Where(clause="deleted=false")
.
🔎 Detailed Explanation
🔹 Example with @Where(clause="deleted=false")
on the User
entity:
Query 1 (HQL style)
List<User> users = session.createQuery("FROM User u", User.class).getResultList();
✅ Hibernate adds WHERE deleted=false
automatically → returns only active users.
Query 2 (JPQL style)
List<User> users = em.createQuery("SELECT u FROM User u", User.class).getResultList();
🔹 Why?
The @Where
annotation is attached to the entity, so whenever you query the entity itself — whether through:
- HQL:
FROM Entity
- JPQL:
SELECT e FROM Entity e
- Criteria API
Hibernate injects the where clause transparently.
🔹 ❗ But watch out:
If your query doesn’t target the entity itself, but rather a DTO or fields, or you use native SQL → then Hibernate won’t inject @Where
.
Example:
// This still works: Hibernate adds WHERE deleted=false
SELECT u.name FROM User u
// But this native query won't include @Where:
SELECT name FROM user
📌 Key Takeaways
✅ Both HQL and JPQL queries targeting the entity will include @Where
automatically.
✅ Don’t worry about writing SELECT u FROM User u
vs FROM User u
→ both will respect @Where
.
❌ Native SQL or non-entity mappings won’t include @Where
→ you must filter manually.