Hibernate HQL Operation – Listing Data in Java
Table of Contents
- Introduction
- Understanding HQL in Hibernate
- Setting Up the Project Environment
- Implementation of HQL “Listing” Operation
- Comparison Table
- Conclusion
Introduction
CRUD stands for:
- Create – Add new records.
- Read – Retrieve existing data.
- Update – Modify data.
- Delete – Remove records.
This article focuses on the “Listing Operation” in Hibernate HQL.
CRUD Table
CRUD Operation | Purpose | Example |
---|---|---|
Create | Add new records | Insert user data |
Read | Fetch existing records | Retrieve user info |
Update | Modify records | Update email |
Delete | Remove records | Delete account |
Understanding HQL in Hibernate
What is HQL?
Hibernate Query Language (HQL) is a query language that is database-independent and operates on entity objects rather than tables.
Key Features of HQL
- Entity-Based: Queries operate on objects, not tables.
- Flexible: Supports SELECT, UPDATE, and DELETE operations.
- Database Independence: HQL queries work across various databases.
Comparison Between HQL and SQL
Feature | HQL | SQL |
---|---|---|
Query Syntax | Object-oriented | Table-based |
Performance | High with caching | Depends on DBMS |
Entity Focus | Operates on entity objects | Works on relational data |
Database Dependency | Independent | Specific to database |
Setting Up the Project Environment
1. Hibernate Configuration File
1 2 3 4 5 6 |
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/test_db root password org.hibernate.dialect.MySQL8Dialect true |
2. 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 37 38 |
package org.studyeasy.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Users { @Id private int id; // Primary Key private String name; // User's Name private String email; // User's Email public Users() {} public Users(int id, String name, String email) { this.id = id; this.name = name; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } |
3. HQL Implementation: 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 35 36 37 38 39 40 41 42 43 44 45 46 |
package org.studyeasy; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import org.studyeasy.entity.Users; import java.util.List; public class App { public static void main(String[] args) { SessionFactory factory = new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Users.class) .buildSessionFactory(); Session session = factory.openSession(); try { // Start transaction Transaction transaction = session.beginTransaction(); // HQL query to list all users String hql = "FROM Users"; Query query = session.createQuery(hql, Users.class); // Execute the query and retrieve the result list List usersList = query.getResultList(); // Display the user data for (Users user : usersList) { System.out.println("ID: " + user.getId() + ", Name: " + user.getName() + ", Email: " + user.getEmail()); } // Commit transaction transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); factory.close(); } } } |
Implementation of HQL “Listing” Operation
Program Output
1 |
New User added successfully! |
Database Table
ID | Name | |
---|---|---|
1 | John Doe | [email protected] |
2 | Jane Smith | [email protected] |
Conclusion
We learned how to implement the “Listing Operation” in Hibernate using HQL. By following the steps above, developers can easily retrieve data from a database without writing complex SQL queries. HQL provides a flexible and database-independent approach for querying data.