Part 4 Hibernate Query Language, Using HQL Select Query

Hibernate » on Jul 8, 2011 { 23 Comments } By Sivateja

Let us see the program on HQL select command,  which is going to cover complete object, partial object (More than one column), partial object (Single column)

here are the required files….

  • (POJO class)
  • Product.hbm.xml  (Xml mapping file )
  • hibernate.cfg.xml  (Xml configuration file)
  • (java file to write our hibernate logic)

package str;

public class Product{

	private int productId;
	private String proName;
	private double price;

	public void setProductId(int productId)
	    this.productId = productId;
	public int getProductId()
	    return productId;

	public void setProName(String proName)
	    this.proName = proName;
	public String getProName()
	    return proName;

	public void setPrice(double price)
	    this.price = price;
	public double getPrice()
	    return price;


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
<property name="connection.url"></property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="">update</property>

<mapping resource="Product.hbm.xml"></mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="str.Product" table="products">

<id name="productId" column="pid"  />
<property name="proName" column="pname" length="10"/>
<property name="price"/>


package str;

import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;

public class ForOurLogic { 

	public static void main(String[] args)

		Configuration cfg = new Configuration();

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();

/* Selecting all objects(records) start_______________________ */


		Query qry = session.createQuery("from Product p");

		List l =qry.list();
		System.out.println("Total Number Of Records : "+l.size());
		Iterator it = l.iterator();

			Object o = (Object);
			Product p = (Product)o;
			System.out.println("Product id : "+p.getProductId());
			System.out.println("Product Name : "+p.getProName());
			System.out.println("Product Price : "+p.getPrice());


/* Selecting all objects(records) end________________________ */		

/* Selecting partial objects(More than one object) start__________ */		


Query qry = session.createQuery("select p.productId,p.proName from Product p");

		List l =qry.list();
		System.out.println("Total Number Of Records : "+l.size());
		Iterator it = l.iterator();

			Object o[] = (Object[]);

			System.out.println("Product id : "+o[0]+ "Product Name : "+o[1]);



/* Selecting partial objects(More than one object)end_____________ */				

// Selecting single object start_____________

        Query qry = session.createQuery("select p.productId
from Product p");

		List l =qry.list();
		System.out.println("Total Number Of Records : "+l.size());
		Iterator it = l.iterator();

			Integer i = (Integer);
			System.out.println("Product id : "+i.intValue());


// selecting single object end____________



Notes regarding

  • CASE 1:  For selecting complete objects (all rows from the table), see from line number 18 – 40 [ just remove the comments ] and check the out put
  • CASE 2:  For selecting partial objects, i mean more than one object, see from line number 42 – 63 [ just remove the comments ] and check the out put
  • CASE 3:  For selecting partial objects,  (selecting single row), see from line number 65 – 82 [ just remove the comments ] and check the out put
  • See the following output just am giving for CASE 3, and remember in case1 i typecast into POJO class type, in case 2 typecast into objects array, in case 3 typecast into that value type__ (in the while loop..)

Eclipse Output 1

Eclipse Output 2

In the database


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


23 Responses to “Part 4 Hibernate Query Language, Using HQL Select Query”
  1. Mariano Costilla says:

    Sweet internet site , super design , really clean and use pleasant.

  2. Daniel says:


    I have just one little question

    What IDE should I have to run the Hibernate-excercices?

  3. Krishna Kumar says:

    Amazing materials for both fresher and experience guys for clearing architecture and logics,maintains da simplicity also.And I think so,anyone can be rocking after go through this site………

  4. Java4s says:


    Actually you can use any enterprise version, am using Eclipse helios

  5. Java4s says:

    @Krishna Kumar

    Thanks for your feedback, hope you will share java4s with your friends.

  6. How to write a query to get the data from 4 different tables. All the tables are having relationships with one another.

  7. Hadi Ubaidillah says:

    greate tutorial,

    Best Regards,
    Hadi Ubaidillah

  8. sreenu says:

    u r very great and very useful u r site.

  9. bhanu says:

    how to update and delete list of records..plz help me

  10. anji says:

    u r very very great and very useful u r site.

  11. Rajitha says:

    How can i retrieve a single column from db using HQL?

  12. Nilesh says:

    in Notes regarding
    CASE 3: For selecting partial objects, (selecting single row),
    is not write. It should be “(selecting single column)” on the place of (“selecting single row”).

  13. Raghavender says:

    Hi excelent information about hibernet, I have one doubt here in the above example In partial object more than one column, we are getting values from object array based on index but these index values are coming from table column or HQL query.
    for more simeple understand:
    select, from Product p

    but in my Product table columns are id next name
    in this case if i call o[0] and o[1] it prints id and name or name and id ?

  14. Sathvik Desu says:

    Excellent Tutorial…… Awesome…….

  15. VG says:

    Nice tutorial to give a kick start on hibernate. Very helpful. Thank you !!

  16. Guest says:

    Nice tutorial. Very helpful.

  17. sailaja says:

    What a wonderful explanation !!!

  18. Srikrit says:

    Guys you don’t believe this i have learnt hibernate in on of the famous institute in hyderabad. when i checked this site once i found that my sir who taught us hibernate totally copied from this site including , and …. also.

  19. swapnil says:

    Awesome tutorial… 🙂
    thnx i leart complete hibernate from this tutorial…

  20. bhagyesha says:

    how to retrieve product from particular category.for example if i am clicking on electronic items then it will show me list of electronics items only

  21. Mukteshwar says:

    I got this error when i try to run hql select query project.thanks in advance

  22. Massey says:

    Getting Error in this package.

    Error: Could not find or load main class


    I got a specific value of primary key say like id I want retrieve remaining records of that id what's the code.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.