Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the query methods of Hibernate in HQL

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report