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..

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

 

Product.java (POJO)

 

Product.hbm.xml

hibernate.cfg.xml

ClientProgram.java

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

Eclipse Output

 

In the database

Note:

  • In this program Product.java is just pojo class nothing special
  • Mapping and Configuration files are just like previous programs
  • But in ClientProgram.java, 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 Product.java(POJO) setter methods, so we are printing by using getter methods

 

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
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.

Comments

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

    Coollll
    Seems you are playing with configuration file, hbm2ddl.auto.

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

  4. Mohammed Vaseem says:

    Hello,
    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 ClientProgram.java
    Object o=session.load(Product.class,new Integer(101));

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

    Thanks and regards

    Vishal

  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()
    .configure(“hibernate.cfg.xml”);
    SessionFactory sf = configuration.buildSessionFactory();
    Session session = sf.openSession();
    //Transaction transaction = session.beginTransaction();
    Product product=(Product) session.get(Product.class, new Integer(200));
    System.out.println(product.getpName());
    System.out.println(product.getPrice());
    System.out.println(product.getProductID());
    //transaction.commit();

  10. arch says:

    Hi vishal
    Answer to your question is , as we have specified PID as in product.hbm.xml.so 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:

    sir,
    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

Name*
Mail*
Website



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
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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