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