Hibernate HQL Operation – WHERE Clause
Table of Contents
- Introduction
- Understanding HQL and WHERE Clause
- Code Implementation
- Code Walkthrough
- Comparison Table
- Conclusion
Introduction
Hibernate Query Language (HQL) is a powerful feature of Hibernate that allows developers to perform SQL-like operations on entity objects. The WHERE clause in HQL is used to filter records based on specific conditions, enhancing flexibility when querying databases.
Key Features:
- HQL is case-insensitive for keywords like FROM, WHERE, etc.
- Enables object-oriented querying, unlike traditional SQL.
When to Use: The WHERE clause is used when specific records need to be fetched that satisfy certain conditions.
Understanding HQL and WHERE Clause
What is HQL?
Hibernate Query Language (HQL) is an object-oriented query language similar to SQL but operates on entity objects rather than database tables.
Where Clause in HQL
The WHERE clause is used to apply filters in a query. This ensures only specific records are fetched based on the condition provided.
Syntax:
1 |
FROM EntityName WHERE condition |
Example:
1 |
FROM Users WHERE username = 'john' OR password LIKE '%123' |
Code Implementation
Entity Class – Users.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.studyeasy.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity(name="users") @Table(name="users") public class Users { @Id @Column(name="user_id") int userId; @Column(name="username") String username; @Column(name="password") String password; @Column(name="first_name") String firstname; @Column(name="last_name") String lastname; public Users() { super(); } @Override public String toString() { return "Users [userId=" + userId + ", username=" + username + ", firstname=" + firstname + ", lastname=" + lastname + "]"; } } |
Main Program – App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package org.studyeasy; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.studyeasy.entity.Users; public class App { public static void main(String[] args) { SessionFactory factory = new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Users.class) .buildSessionFactory(); Session session = factory.getCurrentSession(); try { session.beginTransaction(); // HQL query with WHERE clause List users = session.createQuery("from users where username='john' OR " + "password like '%123'").getResultList(); // Display fetched users for (Users temp : users) { System.out.println(temp); } } finally { session.close(); factory.close(); } } } |
Code Walkthrough
- Entity Class: Maps Java class to the database table.
- Main Class: Retrieves records using the HQL WHERE clause.
- The session.createQuery() method executes the HQL query.
Output
Assume the database contains the following records:
user_id | username | password | first_name | last_name |
---|---|---|---|---|
1 | john | abc123 | John | Doe |
2 | alice | xyz123 | Alice | Smith |
3 | bob | pqr456 | Bob | Brown |
Program Output:
1 2 |
Users [userId=1, username=john, firstname=John, lastname=Doe] Users [userId=2, username=alice, firstname=Alice, lastname=Smith] |
Comparison Table
SQL | HQL |
---|---|
Operates on tables and columns | Operates on entities and fields |
Case-sensitive for table names | Case-insensitive for keywords |
SELECT * FROM users | FROM Users |
Conclusion
The WHERE clause in Hibernate HQL is a crucial feature that enables filtering records efficiently. It simplifies database queries while maintaining a clean, object-oriented approach.