In this article, we will see quick snippets or examples of Hibernate HQL CRUD Example.
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turn perform an action on a database.
HQL is extremely simple to learn and use, and the code is always self-explanatory.
Let's briefly look at the snippets of the HQL CRUD example. For a complete example, check out Hibernate Query Language CRUD Example.
1. HQL Insert Query Example
The below snippet demonstrates the usage of HQL to insert a student:
public void insertStudent() {
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction = session.beginTransaction();
String hql = "INSERT INTO Student (firstName, lastName, email) " +
"SELECT firstName, lastName, email FROM Student";
Query query = session.createQuery(hql);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}
2. HQL Select Query Example
The below snippet demonstrates the usage of HQL for getting a student by id:public Student getStudent(int id) {
Transaction transaction = null;
Student student = null;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction = session.beginTransaction();
// get an student object
String hql = " FROM Student S WHERE S.id = :studentId";
Query query = session.createQuery(hql);
query.setParameter("studentId", id);
List results = query.getResultList();
if (results != null && !results.isEmpty()) {
student = (Student) results.get(0);
}
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
return student;
}
public List < Student > getStudents() {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
return session.createQuery("from Student", Student.class).list();
}
}
3. HQL Update Query Example
The below snippet demonstrates the usage of HQL to update a student's firstName where id = 1:
public void updateStudent(Student student) {
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction = session.beginTransaction();
// save the student object
String hql = "UPDATE Student set firstName = :firstName " + "WHERE id = :studentId";
Query query = session.createQuery(hql);
query.setParameter("firstName", student.getFirstName());
query.setParameter("studentId", 1);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}
4. HQL Delete Query Example
The below snippet demonstrates the usage of HQL to delete a student where id = 1:
public void deleteStudent(int id) {
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction = session.beginTransaction();
// Delete a student object
Student student = session.get(Student.class, id);
if (student != null) {
String hql = "DELETE FROM Student " + "WHERE id = :studentId";
Query query = session.createQuery(hql);
query.setParameter("studentId", id);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
}
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}
Check out a complete example at Hibernate Query Language INSERT, UPDATE, SELECT and DELETE Example
GitHub Repository
The complete source code of this article is available on my GitHub Repository.
Conclusion
In this article, we have seen quick snippets or examples of Hibernate 5 HQL CRUD Example.
You can learn more about Hibernate ORM Framework at Hibernate Tutorial
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course