Hibernate Basic Tools Findbysql Learning

Source: Internet
Author: User

 PublicList<map<string,object>> findbysql (String sql,map<string,object> param,intStartintmax) {Log.debug ("Finding List by Hql"); Try {
The last key to return to the map map SQLQuery query for aliases and database fields=sessionfactory.getcurrentsession (). createsqlquery (SQL); Query.setresulttransformer (TRANSFORMERS.ALIAS_TO_ENTITY_MAP); //traversing param insert Parameters if(param!=NULL) { for(Map.entry<string, object>Entry:param.entrySet ()) {Object obj=Entry.getvalue (); //here, consider what type of arguments are passed in, how different types of methods are used, and how hibernate passes through the list if(objinstanceofCollection<?>) {query.setparameterlist (Entry.getkey (), (Collection<?>) (obj); }Else if(objinstanceofobject[]) {query.setparameterlist (Entry.getkey (), (object[]) obj); }Else{query.setparameter (Entry.getkey (), obj); }}} query.setfirstresult (start);//Start Pageif(max!=0{query.setmaxresults (max);//pages per page} List<Map<String,Object>> results =query.list (); Log.debug ("Find list successful, result size:" +results.size ()); returnresults; } Catch(RuntimeException re) {Log.error ("Find list Failed", re); Throwre; } }

Note that when an array is passed in, int is not part of

Object[] or
Collection<?>
Array incoming required for encapsulation class

Hibernate Basic Tools Findbysql Learning

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.