13.hqlof hibernate (1)

Source: Internet
Author: User

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 ();}}
  



Related Article

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.