HQL parameter bindings, unique results, pagination, projection summary (top)

Source: Internet
Author: User

Let's summarize the common syntax of the HQL statement:

  1. FROM clause:;
  2. Select clause: Used to select objects and attributes;
  3. WHERE clause: The restriction condition used to express the query statement;
  4. Use an expression: generally used in the WHERE clause;
  5. ORDER BY clause: used for sorting;

Here is a summary of the HQL query based on a table of one of my projects:

1. Prepare the data:

Database (Oracle):

1 --Type Table2 Create TableTb_type (3Id Number(4) not NULL Primary Key,4TypeNamevarchar2(Ten)5 6 )7 --Add test Data8 Insert  intoTb_type9 ValuesTen(1,'Comedy'); One Insert  intoTb_type A Values -(2,'Action'); - Insert  intoTb_type the Values -(3,'Love'); - Insert  intoTb_type - Values +(4,'Anime'); - --DVD Information Sheet +  A Create TableTb_dvd at ( -Id Number(4) not NULL, -Namevarchar2( -) not NULL, -Starvarchar2( -) not NULL, -Introvarchar2( -) not NULL, -Price Number(2) not NULL, inNum Number(4) not NULL, -Srcvarchar2( $) not NULL, totypeID Number(2) not NULL +  - ) the  *  $ --Create a foreign keyPanax Notoginseng Alter TableTb_dvdAdd constraintFk_dvd - Foreign Key(typeid) referencing Tb_type (ID); the  + --Create an index A CreateSequence Seq_dvdindex;
DVD Table

2, configure DVD and hibernate mapping relationship

(i) binding parameters in the HQL query statement:

Two different ways:

1, placeholder: "? ”

Hql= "from Dvdentity as where name is like?";

Query.setparameter (0, "%" +emp.getename () + "%");

2. Aliases

Hql= "from Dvdentity as where name Like:name";

Query.setparameter ("name", "%" +emp.getename () + "%");

    

Query has many ways to set parameters:

SetDouble (), Setinteger () .... Wait a minute

I prefer to use the setparameter () mentioned in the demo code above: Set the parameters, do not need to specify the parameter type, very convenient

  

(b) Uniqueresult:

Query queries to a result set, and ResultSet is the same as the wonderful!

Query.list () and. Iteator () are a series of data, and here's someone who asks, if I know that the query results are only likely to be a result, does query provide such a way?

Sure,query.uniqueresult () returns the only result so that no resources are wasted;

Grammar:

      Test test= (Test) query.uniqueresult ();

   

(iii) pagination

    

Paging query:

The next article is detailed!

(d) Projection & dynamic Query

What is a projection:

Sometimes it is not necessary to query all the properties of an object, without learning the hibernate framework money, we use encapsulated entity class, we encapsulate the required data inside, he does not have the complete property, but for the business it is sufficient in the properties, we here to understand the projection to encapsulate a business needs of the entity class, Passing data to the business and accepting the data returned by the business, so the apes are projecting something.

Step one: Create an entity class for business needs (Dvdforprint)

In the entity class, you need a method to construct a parameter that modifies the value

Step Two: Data manipulation

Hql= "Select New Dvdforprint (attribute 1, attribute 2) from dvdentity as DVD where";

List<dvdforprint> list=query.list ();

      Small summary:

More contact, slowly can understand the projection is what, not good explanation!

Dynamic query exactly how dynamic:?

You need to use the FROM clause, WHERE clause, [may use an expression, an by clause]

The code is as follows:

Here I did not refine the Dvdfroprint entity class, directly using the Dvdentity object

1 /**2 * Dynamic Query DVD list3      * @paramDVD4      * @return5      */6      PublicList<dvdentity>Getdvdbyhiber (dvdentity DVD) {7List<dvdentity> list=NewArraylist<dvdentity>();8         //hql9StringBuffer hql=NewStringBuffer ("From Dvdentity where 1=1");Ten         Try { Oneconf=NewConfiguration (). Configure (); Afactory=conf.buildsessionfactory (); -session=factory.opensession (); -query=session.createquery (APPENDHQL (DVD,HQL). toString ()); the             //Specify DVD Object - query.setproperties (DVD); -list=query.list (); -}Catch(Exception e) { +             //Todo:handle Exception - e.printstacktrace (); +}finally{ A              at session.close (); -         } -          -          -         returnlist; -          in          -     } to     /** + * Stitching HQL -      * @paramDVD the      * @paramhql *      * @return $      */Panax Notoginseng     Privatestringbuffer appendhql (dvdentity dvd,stringbuffer hql) { -         if(Dvd.getname ()! =NULL){ theHql.append ("and name Like:name"); +              A}if(Dvd.getintro ()! =NULL){ theHql.append ("and Intro Like:intro"); +              -}if(Dvd.getstar ()! =NULL){ $Hql.append ("and star Like:star"); $              -}if(Dvd.getprice ()! =NULL){ -Hql.append ("and Price Between:starprice And:endprice"); the              -}if(Dvd.gettypeid ()! =NULL){WuyiHql.append ("and Typeid=:typeid"); the              -         } Wu         if(Dvd.getnum ()! =NULL){ -             if(Dvd.getnum () >0){ AboutHql.append ("ORDER BY num DESC"); $                  -             } -              -         } A         returnhql; +          the     } -     
View Code

Experience Summary:

Not currently

      

HQL parameter bindings, unique results, pagination, projection summary (top)

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.