How To Fetch Data From MySQL table In Java Using Hibernate



In this post, I will be showing you how to fetch data from MySQL table and show it in the console of your IDE using Hibernate and Java.
If you have chosen Hibernate version 3.0 in your DTD in hibernate.cfg.xml file and if you refer to https://developer.jboss.org/docs/DOC-15062#api. you will find that query execution methods like find(), iterate(), filter() and delete() has been deprecated instead we'll be using createQuery() method. Later in this document, you will also get error for using Query class as it is deprecated but don't worry about it as it will still work. You can also read the document above completely to understand what new features are released and which ones have been removed in newer version.

Suppose you have a Student class:

@Entity
public class Student{
    @Id
    private String id;

    private String name;

//getters and setters here
:
:
:
}

and you have the following data in your Student MySQL table.
ID|Name
1 |Sumit
2 |Amit
3 |Saurav

So what SELECT statement would type if you want to display the whole table? It will be "SELECT * FROM STUDENT;", right? So this is SQL - Structured Query Language. What if you wanted to print the whole table in your Java program using Hibernate. Then there you would use HQL - Hibernate Query Language. It is similar to SQL but instead of working on columns and tables, it works with persistent objects and their properties. HQL queries are translated by Hibernate into traditional SQL queries. You can read more about HQL later.

So HQL for above SQL will be:
Query q = session.createQuery("From Student");

or you can type class name with proper path (package):
Query q = session.createQuery("FROM in.sumitkp.Student");

You can then store the result returning from MySQL in a list:
List<Student> results = q.list();
for(Student s : results){
    System.out.println(s);
}

So, this will print the whole table in the console but what if you wanted to print ID and Name of "Sumit" only. In SQL, we would be using, "SELECT * FROM STUDENT WHERE NAME="Sumit";" but in HQL, you cannot use this if you have to take input from the user:
Query q = session.createQuery("FROM Student WHERE NAME="Sumit");
or
Query q = session.createQuery("FROM Student WHERE NAME=\""+name+"\"";

This will give an error if you run it using an escape sequence for double-quotes. For this, we will be using .setParameter.
If you are taking String name input from user, then the query for it will be:

Query q = session.createQuery("FROM Student WHERE NAME = :Name).setParameter("Name", name);
List<Student> results = q.list();
for(Student s : results){
    System.out.println("ID: "+s.getId()+"\nName: "+s.getName());
}

Here, inside setParameter method, Name is the parameter you want your value to replace with inside createQuery and name is String variable which will contain input from the user.

Tip: Select an opening or closing bracket '{' or '}' and press CTRL + SHIFT + P on your keyboard to find its matching bracket.


Comments

Popular

How To Create MySQL Table with SequelizeJS

How To Read CSV Files in Java as Maven Project

How To Create A Hibernate Project in Maven