Get all updates of Java4s

Hibernate Criteria, Adding Conditions To Criteria

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

If we want to add some sorting order for the objects, before the objects are going to store in list object then we need to add an Order class object to the Criteria class object by calling addOrder() method..,

  • Order is a class given in “org.hibernate.Criterion” package
  • In Order class, we have 2 static methods, asc()[ascending order] and dsc()[descending order] for getting an objects in required order
  • Internal concept is, hibernate will select the records (rows) from PRODUCT table and stores them into a ResultSet and then converts each row data of resultset into a POJO class object basing on our field type, then all these objects into a list according to the order you have given

Let’s see an example..

required file…

  • class)
  • Product.hbm.xml
  • hibernate.cfg.xml
  • (For writing our business logic)




  • In line number 27, used Restrictions to get the price greater then 17000, and remember here price is the pojo class variable
  • In line number 28, added criterion objects to criteria object
  • line number 29 its our current program concept,  we are calling addOrder() method to get the price in assenting order
  • We can use for proName also, its depends on your requirement


You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web 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.


8 Responses to “Hibernate Criteria, Adding Conditions To Criteria”
  1. Anand says:

    This tutorial is very very good….it is really very very helpful from understanding java tech…many many thanxxx to you Sir…

  2. Java4s says:


    Glad to hear that it helped you much :-), thanks for your feedback.

  3. hi sir, in hibernate application when i got offline then this error occur ,why?….log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.configure(
    at com.action.ForOurLogic.main(
    Caused by: org.dom4j.DocumentException: Nested exception:
    at org.hibernate.cfg.Configuration.doConfigure(
    … 2 more

    ,sir huge thanks for supporting us and always be…………..

  4. avneesh says:

    Before some time I was new to java and hibernate. But this link guide me step by step. so now I am comfortable with these things.
    Its very useful for fresher.
    Thanks a lot sir for this tutorial

  5. karthick says:

    very use full to me….could u please explain this concept in multiple condition scenario….thanx in advance…

  6. karthick says:

    could u please tell me what are all methods available in Restriction interface…thanx in advance…

  7. john says:

    HI can you explain this Product p=(Product); not able to understand object type casting why i need to do this

  8. naresh says:

    normally returns object type to convert into product type we need to type cast it


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

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