Hibernate Query Langugae (HQL)

Hibernate Query Language:

  • HQL is one of the feature of Hibernate.
  • HQL is same like SQL but here it uses class name as table name and variables as columns.
  • HQL is Database independent query language.
  • An object oriented form of SQL is called HQL
  • HQL syntax is very much similar to SQL syntax.  Hibernate Query Language queries are formed by using Entities and their properties, where as SQL quires are formed by using Tables and their columns.
  • HQL Queries are fully object oriented..
  • HQL Queries are case sensitive.


Advantages of HQL:

  • Database Independent
  • HQL Queries supports inheritance and polymorphism
  • Easy to learn. 

Query Interface:

  • Query interface used to Represent HQL query in the form of query object.
  • The object of  query will be created by calling createQuery(hql query) method of session.

  1.  Session hsession = sf.openSession();
  2. Query query = hsession.createQuery(hql query);

  • Send the query object to hibernate software by calling the list method.
  • Hibernate returns an ArrayList object render the ArrayList object and display the output to client.

  1. List l = query.list();
  2. ArrayList emplist = (ArrayList)l;

  • Query is an interface which is available as port of org.hibernate package. We can not create the object to query interface. We can create a reference variable and it holds implementation class object.

 Methods of Query Interface:

HQL hibernate query language

Hibernate supported databases List

  • Hibernate is an ope source framework and also called as an ORM tool.
  • Hibernate supports lot of databases. 
  • Please find below list of databases that are supported by hibernate.
  • Hibernate supported databases list 


  1. DB2    
  2. DB2 AS/400   
  3. DB2 OS390 
  4. FrontBase
  5. Firebird  
  6. HypersonicSQL 
  7. H2 Database  
  8. Informix   
  9. Ingres  
  10. Interbase
  11. MySQL5    
  12. MySQL5 with InnoDB    
  13. MySQL with MyISAM    
  14. Mckoi SQL
  15. Microsoft SQL Server 2000    
  16. Microsoft SQL Server 2005  
  17. Microsoft SQL Server 2008  
  18. Oracle
  19. Oracle 9i   
  20. Oracle 10g    
  21. Oracle 11g      
  22. PostgreSQL  
  23. Progress   
  24. Pointbase
  25. SAP DB
  26. Sybase
  27. Sybase 

  •  Following is the list of various important databases dialects for corresponding database.

hibernate supported databases

Hibernate configuration file

1.hibernate.dialect:
  • This property hibernate.dialect makes Hibernate generate the appropriate SQL for the given database.
2.hibernate.connection.driver_class:
  • This property hibernate.connection.driver_class specifies jdbc driver class name 
3.hibernate.connection.username:
  • This property hibernate.connection.username specifies database user name.
4.hibernate.connection.password:
  • This property hibernate.connection.password specifies password.



hibernate.cfg.xml file structure


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration>
  6.  
  7. <session-factory>
  8.  
  9. <!-- Related to the connection START -->
  10. <property name="connection.driver_class">Driver Class Name </property>
  11. <property name="connection.url">URL </property>
  12. <property name="connection.user">user name</property>
  13. <property name="connection.password">password</property>
  14. <!-- Related to the connection END -->

  15. <!-- Related to hibernate properties START -->
  16. <property name="show_sql">true/false</property>
  17. <property name="dialet">Database dialet class</property>
  18. <property name="hbm2ddl.auto">create/update or what ever</property>
  19. <property name="hibernate.jdbc.batch_size">hibernate container that every N rows to be
  20. inserted as batch.</property> 
  21. <!-- Related to hibernate properties END-->
  22.  
  23. <!-- Related to mapping START-->
  24. <mapping resource="hbm file 1 name .xml" />
  25. <mapping resource="hbm file 2 name .xml" />
  26. <!-- Related to the mapping END -->
  27.  
  28. </session-factory>
  29.  
  30. </hibernate-configuration>




Example hibernate.cfg.xml. file  for mysql


hibernate configuration file java

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="connection.url"> jdbc:mysql://localhost/test</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.MySQLDialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>


Example hibernate.cfg.xml. file for oracle Db

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  9. <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:xe</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.Oracle9Dialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>



Select Menu