13.hqlof hibernate (1)
Query all data:
@Testpublic void queryAllTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student";Query query=session.createQuery(hql);List
list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
Parameter-Based Query:
@Testpublic void queryWithParamTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student where studentId=?";Query query=session.createQuery(hql);query.setInteger(0, 83);List
list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
Query Based on Command Parameters
@Testpublic void queryWithEqualsNamedParamTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student where studentId=:studentId";Query query=session.createQuery(hql);query.setInteger("studentId", 83);List
list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
Like:
@ Testpublic void queryWithLikeTest () {Session session = null; try {session = HibernateUtil. openSession (); String hql = "from Student where studentName like? "; Query query = session. createQuery (hql); query. setString (0," % Wang % "); List
List = query. list (); for (Student student: list) {System. out. println (student) ;}finally {session. close ();}}
In:
@Testpublic void queryWithInTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student where studentId in (:stuIds)";Query query=session.createQuery(hql);query.setParameterList("stuIds", new Integer[]{63,83});List
list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
Query some fields:
@ Testpublic void queryColumnsTest () {Session session = null; try {session = HibernateUtil. openSession (); String hql = "select studentId, studentName from Student where studentName like? "; Query query = session. createQuery (hql); query. setString (0," % Wang % "); List
List = query. list (); for (Object [] objs: list) {System. out. println (objs [0] + "" + objs [1]) ;}} finally {session. close ();}}
Query a single field:
@ Testpublic void querySingleColumnTest () {Session session = null; try {session = HibernateUtil. openSession (); String hql = "select studentName from Student where studentName like? "; Query query = session. createQuery (hql); query. setString (0," % Wang % "); List
List = query. list (); for (String name: list) {System. out. println (name) ;}} finally {session. close ();}}
Query statistics:
@ Testpublic void queryCountTest () {Session session = null; try {session = HibernateUtil. openSession (); String hql = "select count (*) from Student where studentName like? "; Query query = session. createQuery (hql); query. setString (0," % Wang % "); List
List = query. list (); for (Long cnt: list) {System. out. println (cnt) ;}} finally {session. close ();}}
Single result set
@Testpublic void queryUniqueResultTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="select stu from Student stu where stu.studentId=?";Query query=session.createQuery(hql);query.setInteger(0, 83);Student stu=(Student)query.uniqueResult();System.out.println(stu);}finally{session.close();}}
Paging query:
@ Testpublic void queryPagerTest () {Session session = null; try {session = HibernateUtil. openSession (); String hql = "select stu from Student stu where studentName like? "; Query query = session. createQuery (hql); // query the number of records. setFirstResult (1); // query the size of each page. setMaxResults (20); query. setString (0, "% Wang %"); List
List = query. list (); for (Student stu: list) {System. out. println (stu) ;}finally {session. close ();}}