Hibernate QBC retrieval and local SQL retrieval

Source: Internet
Author: User

QBC queries are querying objects by using the query by Criteria API provided by Hibernate, which encapsulates the dynamic assembly of SQL statements and provides a more object-oriented functional interface to queries.

Local SQL queries to refine HQL cannot cover all of the query features.

==================== Code Area ======================

Test class

1  Packagecom.yl.hibernate.test;2 3 4 Importjava.util.ArrayList;5 Importjava.util.Arrays;6 ImportJava.util.LinkedHashSet;7 Importjava.util.List;8 ImportJava.util.Set;9 Ten ImportORACLE.NET.ASO.E; One  A ImportOrg.hibernate.Criteria; - ImportOrg.hibernate.Query; - Importorg.hibernate.Session; the Importorg.hibernate.SessionFactory; - Importorg.hibernate.Transaction; - Importorg.hibernate.cfg.Configuration; - Importorg.hibernate.criterion.Conjunction; + Importorg.hibernate.criterion.Disjunction; - ImportOrg.hibernate.criterion.MatchMode; + ImportOrg.hibernate.criterion.Order; A Importorg.hibernate.criterion.Projection; at Importorg.hibernate.criterion.Projections; - Importorg.hibernate.criterion.Restrictions; - ImportOrg.hibernate.service.ServiceRegistry; - ImportOrg.hibernate.service.ServiceRegistryBuilder; - ImportOrg.junit.After; - ImportOrg.junit.Before; in Importorg.junit.Test; -  to Importcom.yl.hibernate.entities.Department; + ImportCom.yl.hibernate.entities.Employee; -  the  Public classHibernatetest { *  $     Privatesessionfactory sessionfactory;Panax Notoginseng     PrivateSession session; -     PrivateTransaction Transaction; the      + @Before A      Public voidinit () { theConfiguration Configuration =NewConfiguration (). Configure (); +Serviceregistry Serviceregistry = -                 NewServiceregistrybuilder (). Applysettings (Configuration.getproperties ()) $ . Buildserviceregistry (); $  -Sessionfactory =configuration.buildsessionfactory (serviceregistry); -          theSession =sessionfactory.opensession (); - WuyiTransaction =session.begintransaction (); the     } - @After Wu      Public voiddestory () { - transaction.commit (); About          $ session.close (); -          - sessionfactory.close (); -     } A      +      the @Test -      Public voidTESTQBC () { $         //1. Create a Ctiteria object theCriteria = Session.createcriteria (Employee.class); the         //2. Add a query condition: query criteria in QBC use criterion to represent the         //criterion can be obtained by restrictions static method. theCriteria.add (Restrictions.eq ("email", "president")); -Criteria.add (restrictions.gt ("salary", 500f)); in         //3. Execute the query theEmployee Employee =(Employee) Criteria.uniqueresult (); the SYSTEM.OUT.PRINTLN (employee); About     } the      the @Test the      Public voidtestQBC2 () { +          -Criteria = Session.createcriteria (Employee.class); the         //1.AND: Use conjunction to indicateBayi         //conjunction itself is a criterion object the         //and you can also add criterion objects theconjunction conjunction =restrictions.conjunction (); -Conjunction.add (Restrictions.like ("name", "A", Matchmode.anywhere)); -Department Dept =NewDepartment (); theDept.setid (20); theConjunction.add (Restrictions.eq ("dept", dept)); the SYSTEM.OUT.PRINTLN (conjunction); the          -         //2. OR theDisjunction disjunction =restrictions.disjunction (); theDisjunction.add (restrictions.ge ("salary", 1000F)); theDisjunction.add (restrictions.isnotnull ("email"));94          the Criteria.add (disjunction); the Criteria.add (conjunction); the         98 criteria.list (); About         /** - * where101         (102 This_. Salary>=? 103 or This_. EMAIL is not null104         )  the and (106 this_.name like?107 and This_. Dept_id=?108         )109          */ the     }111      the @Test113      Public voidtestQBC3 () { theCriteria = Session.createcriteria (Employee.class); the          the         //statistical query: Using projection representation: can be obtained by the static method of projections117Criteria.setprojection (Projections.max ("salary"));118         119 System.out.println (Criteria.uniqueresult ()); -     }121     122 @Test123      Public voidtestQBC4 () {124Criteria = Session.createcriteria (Employee.class); the         126         //1. Add Sort127Criteria.addorder (ORDER.ASC ("salary")); -Criteria.addorder (Order.desc ("email"));129          the         //2. Add Pagination131         intPageSize = 3; the         intPageNo = 2;133Criteria.setfirstresult ((pageNo-1) *pageSize)134 . Setmaxresults (pageSize)135 . List ();136         137     }138     139 @Test $      Public voidtestnative () {141String sql = "INSERT into yl_department VALUES (?,?)";142Query query =session.createsqlquery (SQL);143         144Query.setinteger (0, 50)145. setString (1, "DEV")146 . executeupdate ();147     }148     149 @Test Max      Public voidtesthqlupdate () {151String hql = "DELETE from Department d WHERE d.id =: id"; the         153Session.createquery (HQL). Setinteger ("id", 50)154 . executeupdate ();155         156     }157}

Hibernate QBC retrieval and local SQL retrieval

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.