15, Hibernate's native SQL query, optimize hibernate query efficiency

Source: Internet
Author: User

 Public classTestsql {@Test Public voidtest01 () {Session session=NULL; Try{Session=hibernateutil.opensession (); List<Student> Stus = Session.createsqlquery ("select * from T_stu where name like?"). addentity (Student.class). Setparameter (0, "% hole%"). Setfirstresult (0). Setmaxresults (10). List ();  for(Student stu:stus) {System.out.println (Stu.getname ()); }        } Catch(Exception e) {e.printstacktrace (); } finally{Hibernateutil.close (session); }} @Test Public voidtest02 () {Session session=NULL; Try{Session=hibernateutil.opensession (); List<Object[]> Stus = Session.createsqlquery ("Select {stu.*},{cla.*},{spe.*} from" + "T_stu Stu Left Join T_classroom CLA on (stu.cid=cla.id) "+" left join T_special SPE on (spe.id=cla.spe_id) where STU.N Ame like? "). Addentity ("Stu", Student.class). Addentity ("CLA", classroom.class). Addentity ("SPE", special.class). Setparameter (0, "% hole%"). Setfirstresult (0). Setmaxresults (10). List (); Student Stu=NULL; Classroom CLA=NULL; Special SPE=NULL; List<StuDto> list =NewArraylist<studto>();  for(object[] obj:stus) {stu= (Student) obj[0]; CLA= (classroom) obj[1]; SPE= (Special) obj[2]; List.add (Newstudto (Stu, CLA, SPE)); }        } Catch(Exception e) {e.printstacktrace (); } finally{Hibernateutil.close (session); }} @Test Public voidtest03 () {Session session=NULL; Try{Session=hibernateutil.opensession (); List<StudentDto> Stus = Session.createsqlquery ("Select Stu.id as sid,stu.name as sname," + "stu.sex A S sex,cla.name as cname,spe.name as Spename from "+" T_stu Stu left join T_classroom CLA on (stu.cid=cl a.id) "+" left join T_special SPE in (spe.id=cla.spe_id) where stu.name like? "). Setresulttransformer (Transformers.aliastobean (studentdto.class). Setparameter (0, "% hole%"). Setfirstresult (0). Setmaxresults (10). List ();  for(Studentdto sd:stus) {System.out.println (SD); }        } Catch(Exception e) {e.printstacktrace (); } finally{Hibernateutil.close (session); }    }            }

15, Hibernate's native SQL query, optimize hibernate query efficiency

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.