Get all updates of Java4s
Follow Us: Email Newsletter

Example On Hibernate Select Query

Hibernate » On Jun 12, 2011 { 13 Comments } By Sivateja

This is an example for loading the object from the database remember in the hibernate loading 1 object from the database means applying select command (select * from _____) for fetching one complete record from the database.

Files required to execute this program..

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




Now compile all .java files and run to see the output

Eclipse Output


In the database


  • In this program is just pojo class nothing special
  • Mapping and Configuration files are just like previous programs
  • But in, see in line number 16 load(-,-) method which is in the session, actually we have 2 methods to load the object from the database, they are load and get i will explain when time comes, as of now just remember this point
  • Now see line number 19,  we are going to print the product name by writing +s.getProName
  • Actually once we loaded the object for the database with load or get methods the object data will be loads into the setter methods, so we are printing by using getter methods


You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


13 Responses to “Example On Hibernate Select Query”
  1. Mohammed Vaseem says:

    Hello Java4s,
    I modified this program a little bit and getting some exceptions.
    Taken Product class attributes as
    private int productId;
    private String productName;
    private int productPrice;

    Configured this Product.class in Products.hbml.xml file. Products.hbml.xml is successfully configured in hibernate.cfg.xml file.

    In your program @ line 19, we are retrieving object from database via a class object. for this we used
    System.out.println(“Loaded object product name is___”+s.getProName()); (as per your program)

    My doubt:
    i wrote in my program like this,
    System.out.println(“Product id is : “+p.getProductId());
    System.out.println(“Product name is : “+p.getProductName());

    Product id is printing successfuly but productname is not printing and showing exception as
    No row with the given identifier exists: [Product#101]

    What is the reason behind this? Please help.

  2. Mohammed Vaseem says:

    I got solution for this. My mistake is, i kept
    create but not update.
    So every time whenever the program is execution is commenced, a new table is being created by deleting the previous table. So newly created table is always empty. I think am right.

  3. Java4s says:

    @ Vaseem

    Seems you are playing with configuration file,

    You are correct, actually you are not specified that you changed configuration file, that’s why i little confused.

  4. Mohammed Vaseem says:

    I learning is all about playing… and I think We can learn better by playing only.

  5. Veera says:

    how can i know which database name i’ll choose in oracle database….

  6. Vishal says:

    Hi Admin,

    First of All Thank you very Much for this.

    I have one doubt in Line no. 21 of
    Object o=session.load(Product.class,new Integer(101));

    How the Hibernate ensures that Integer 101 belongs to PID itself??

    Thanks and regards


  7. prasad says:

    hi boss
    i have a doubt single row display is good,
    how to display total table in a specific database

  8. Sourov says:

    Object o = session.load(Product.class,new Integer(101));
    Product s = (Product)o;

    Since I have fetched ‘Product.class’,
    then can u make me clear pls, why did u cast here?

  9. Narasimha says:

    Hi sir,

    for load and get no need transaction object this for your information..please clarify is i am wrong

    Configuration configuration = new Configuration()
    SessionFactory sf = configuration.buildSessionFactory();
    Session session = sf.openSession();
    //Transaction transaction = session.beginTransaction();
    Product product=(Product) session.get(Product.class, new Integer(200));

  10. arch says:

    Hi vishal
    Answer to your question is , as we have specified PID as in this select query will select the row on the basis of PID .thats why hibernate ensures Interger 101 as PID .

  11. Rajyalaxmi says:

    There is nothing wrong in your code and if u want to fetch complete table info from DB then u shud go for HQL,this is for bulk operations

    This wil also work:::::::::::
    Instead of Product p=(Product)session.get(“Product”,101);
    Query query=session.createQuery(“from Product p”);

  12. silambarasu says:

    Could not parse configuration: hibernate.cfg.xml
    i run in linux command prompt.i put all source file in same folder.logs files are stored.tell me sir


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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