Hibernate query instances in HQL

Source: Internet
Author: User

We learn hibernate query is to know HQL support condition query, divided into many aspects, here we have a specific hibernate query with examples to explain, hope to help the reader's study.

1. Support string method parameter pass query:

Example:

List students = session.createQuery("select s.id,s.name from Student s where s.name like '%1' ").list();
             for (Iterator iter=students.iterator(); iter.hasNext();) {
                 Object[] o = (Object[])iter.next();
                 System.out.println(o[0]+","+o[1]);
             }

Because there are multiple property queries involved, the object array type is returned.

2, support similar to the preparestatement way? Pass parameter query

Example:

List students = session.createQuery("select s.id,s.name from Student s where s.name like :goodname ")
                                                 .setParameter("goodname", "%1%").list();
             for (Iterator iter=students.iterator(); iter.hasNext();) {
                Object[] o = (Object[])iter.next();
                System.out.println(o[0]+","+o[1]);
            }

To set the parameter starting at 0, see Line 2nd.

3, go variable mode parameter transfer

Example:

List students = session.createQuery("select s.id,s.name from Student s where s.name like :goodname ")
                                                 .setParameter("goodname", "%1%").list();
             for (Iterator iter=students.iterator(); iter.hasNext();) {
                Object[] o = (Object[])iter.next();
                 System.out.println(o[0]+","+o[1]);
            }

This is similar to the one in 2, except that many parameters can be grouped into a class.

4. Support multi-parameter transmission

Example:

List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")
             .setParameterList("myids", new Object[]{1, 2, 3, 4, 5,32,13,14})
                 .list();
             for (Iterator iter=students.iterator(); iter.hasNext();) {
                 Object[] o = (Object[])iter.next();
                 System.out.println(o[0]+","+o[1]);
             }

Note that the calling method is Setparameterlist.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.