Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the query methods of Hibernate in HQL

Shulou Source: shulou.com Published: 2022-06-02 08:03:50 10月15日 Update

This article introduces the relevant knowledge of "what are the query methods of Hibernate in HQL". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Support parameter passing query in string mode:

Example:

List students = session.createQuery ("select s.iddres.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 multiple attribute queries are involved, the Object array type is returned.

2. Support something similar to PrepareStatement? Pass parameter query

Example:

List students = session.createQuery ("select s.iddres.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 from 0, see line 2.

3. Parameter transfer in de-variable mode

Example:

List students = session.createQuery ("select s.iddres.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 the extra parameters can be grouped into one category.

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 Iterator iter=students.iterator (); iter.hasNext ();) {Object [] o = (Object []) iter.next () System.out.println (o [0] + "," + o [1]);}

Notice that the calling method is setParameterList.

5. Call the functions in the database

Example:

List students = session.createQuery ("select s.id, s.name from Student s where date_format (s.createTime,'% YMY% m') =?") .setParameter (0, "2008-10") .list (); for (Iterator iter=students.iterator (); iter.hasNext ();) {Object [] o = (Object []) iter.next () System.out.println (o [0] + "," + o [1]);}

Database function date_format.

6. Directly support sql query

Example:

List students = session.createSQLQuery ("select * from t_student"). List (); for (Iterator iter=students.iterator (); iter.hasNext ();) {Object [] o = (Object []) iter.next (); System.out.println (o [0] + "," + o [1]);}

You just need to call the createSQLQuery method in session.

7. Make navigation query

Example:

List students = session.createQuery ("select s.name from Student s where s.classes.name like'% 1%'"). List (); for (Iterator iter=students.iterator (); iter.hasNext ();) {String s = (String) iter.next (); System.out.println (s);}

This kind of inquiry is very convenient.

8. Paging query

Example:

List students = session.createQuery ("from Student"). SetFirstResult (0) .setMaxResults (3). List (); for (Iterator iter=students.iterator (); iter.hasNext ();) {Student student = (Student) iter.next (); System.out.println (student.getName ());}

This is a very simple paging query, the implementation of paging query is much more complex, paging query is equivalent to the limit in the database, to limit the number of entries displayed in a query.

This is the end of the content of "what are the query methods of Hibernate in HQL". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Queries examples parameters methods support data methods content functions more knowledge faceted library complex practical similar learned and then variables just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia MySQL macOS Docker