Part 5 Hibernate Query Language, Passing Runtime Values

Now we will see, how to pass the values at time time while using the HQL  select query, actually same concept for 3 cases.

Required files…

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






Eclipse Console

In The Database


  • As i discussed earlier, in the hibernate we can pass the values into the hibernate query at run time also
  • See the commented code from line numbers 1839,  that if we would like to use Label rather question mark symbol
  • See the HQL query in line number 20, i have been used label :java4s [ as a programmer its our responsible to let the hibernate to know its the label not the value, by specifying the colon(:) symbol at starting of label ]
  • In line number 21 i used setParameter method to give the value that’s it.
  • And if you observer line number 41, i used question mark symbol rather label, this time in setParameter method i used zero (0) as first parameter and value in second parameter, remember if use more than one question mark symbol in the query then also the index will starts from zero only


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.


11 Responses to “Part 5 Hibernate Query Language, Passing Runtime Values”
  1. Luisa Sedgwick says:

    You got a very superb website, Sword lily I noticed it through yahoo.

  2. utkal samal says:

    Please let me know ,when we pass a runtime value as a Level or as a Question mark? which one is the better approach ?or which one is the recomanded ??????

  3. Java4s says:

    @Utkal samal

    Actually its depends on you friend.
    But i always recommend question marks, as its quick ;) in most of the real time projects i saw question marks only.

  4. Neeraj+Kashyap says:

    How it is passing the value at runtime coz we are doing setparameter. I mean we have hardcoded the identifier in the next line then how its dynamic query build?

  5. vijay says:

    why parameter start from 0

  6. keshav says:

    sir, setParameter(- ,-) method in which class method.
    thank u for advance

  7. Arpita says:

    Really nice site…Thank you so much guys..for making it so simple

  8. Bharat says:

    As you mentioned above in notes, " remember if use more than one question mark symbol in the query then also the index will starts from zero only". So I want to know How to pass multiple "?" symbols in query.

  9. Anirban says:

    How to pass two run time value in hql.
    the below code does nor working:
    public List<Data> getData(Long id){
    List<Data> list=new ArreyList<Data>();
    SOP("Inside Try");
    list=template.find(from Data t where t.acc_no=? and t.status=?, id, "STATIC" );
    } catch(Exception e)
    SOP("" + e.getMessage());
    return list;

    Kindly answer ASAP


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Hibernate Spring Struts
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.