Showing posts with label HQL. Show all posts
Showing posts with label HQL. Show all posts

Friday, July 17, 2015

HQL (Hibernate Query Language) – FROM Clause

Hibernate Provides a very good Query Language HQL (Hibernate Query Language) in the form of OOPs. Hibernate is very much similar to the SQL but it follow the OOPs strictly so you have to be very carefull about it. So we will start with few basic example to understand how to write the HQL Queries.
FROM Clause (SELECT Query)
SQL : – select * from person
HQL : – 1. from com.hibernate.enity.Person
           2. from com.hibernate.enity.Person p
           3. select p from com.hibernate.enity.Person p
All the above three HQL queries will result the same output. So you can say the SQL query(select * from person) is similar to all the above three HQL queries.Now we will take each keyword from Hibernate query and will try to understand
1. from com.hibernate.enity.Person
from : – It is a keyword just similar to SQL query
com.hibernate.enity.Person : – You have to specify the complete package name along with the class name.(* package name should be the exact and Class name first letter should be capital)
2. from com.hibernate.enity.Person p
from : – It is a keyword just similar to SQL query
com.hibernate.enity.Person p : – Here p is an alias which we have created for the class Person rest of the details are same as explained above
3. select p from com.hibernate.enity.Person p
After reading the above points 1,2 this query will be self explanatory for you.
Please follow the code sample below : -
1.Project Structure

 
2. FROM clause code
/* Hibernate select query Example */
    public void selectFromTable(){
         
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         
        //begin the transaction
        Transaction trans=session.beginTransaction();
         
        //select query example
        String selectQuery = "select p from com.hibernate.enity.Person p";
        Query query = session.createQuery(selectQuery);
         
        //select query will return multiple result set.
        //create a list : -List personList and cast the output to store into the personList
        List personList = (List)query.list();
         
         
        //print the results for the personList
        for(Person personObj:personList){
             
            System.out.println("Id : -  "+personObj.getId()+" First Name : - "+personObj.getFirstName()+" Last Name : - "+personObj.getLastName());
        }
         
         
    }
 
3.MYSQL TABLE RECORDS
 
MYSQL data records of person table  

Thursday, July 16, 2015

What is Hibernate ORM(object-relational mapping)?

Hibernate ORM ( Object Relational Mapping) in the short form called as Hibernate is a framework which is being used for performing all the tasks which are related to Database operations.
So seems like quite complex but do not worry just go through the following points and than you will get to know more above Hibernate : -
  1. OOP : -Main aim of hibernate is to convert the basic SQL queries and operations which we do into the Object Oriented Paradigm.
  2. JAVA POJO(Plain Old Java Object) : - All of your database table will be represented with the JAVA class know as POJO(Plain Old Java Object)
  3. NO SQL : - So you do not have to deal with SQL queries all you need is to play with the JAVA POJO classes.
  4. HQL(Hibernate Query Language) : -Hibernate provides HQL (Hibernate Query Language) which is much a like SQL(Structured Query Language) for writing the database queries for performing Database operations.
  5. INTEGRATION : – Hibernate is quite good at integration and it can be integrated with any JAVA, JAVA EE, EJB, JBI application.
If you are new to the Hibernate and beginner in the JAVA than it will look like a lot in your plate to eat. But do not worry we will have each bite one by one. So seat tight we have lot of topics which will explain you how to start working with the Hibernate.