A brief talk on Hibernate--hibernate inquiry (query Language) (GO)

Source: Internet
Author: User

1. Querying Objects: Query has Org.hibernate.query and Javax.persistence.query,Use the following


If you search by criteria



2. Placeholder: such as the following example: Min and: Max, and Query.setparameter (Key,value);


Chained can also be, while here Setparameter ("", "") is automatically set the value of the parameter, if you know the incoming type can be setinteger.



3. Pagination Effect
Remove records between M~n by Query.setmaxresult (n). Setfirstresult (M)

Setfirstresult (M): Choose to start from the first few lines, the default is Row0
Setmaxresult (n): Determines how many records to remove, by default all rows


Same as the following common QL statement implementation


4. Get Child properties
Query Query=session.createquery ("from score s where s.xxx.id= ...")


You can also take a temporary object out of it and put it in an object or a helper class that you build.
1.Query query=session.createquery ("Select S.name,c.name from Student s,course C where s.id=.. and c.id=. ")
2.Query query=session.createquery ("Select New Vobj (s.name,c.name) from Student s,course C where s.id=. and c.id=. ")

It is undesirable to take out some of the attributes that are encapsulated into a new object, preferably taking the entire attribute out to ensure the relevance of his logic
If you want to remove it, take it.



5. Single Object Matching
If you know that the only object is taken out, then you do not need to query.list (), use Query.uniqueresult (); High efficiency



COUNT (*) returns a long type


Inch


Group by
Select AVG (s.age) from Student Group by S.col;

Having
Select COUNT (*) from Student s where group by S.col have Count (*) >3;
Null value empty: Below is the Student Class (Student) has set<course> courses attribute (Student selected course) Choose which courses are empty students


Like:% more; _ A


Distinct





6. The QL statements can be put together in order to maintain the modification with @namedqueries and @namedquery, when called with Session.getnamedquery (queryname)




7. Using the database's own language
If you can not solve the problem with HQL, then use the SQL language to solve, through Createsqlquery () to search the table, and Addentity (xx. Class) into an object


Of course, this can also be used with a named query



8. Bulk Delete: by Query.executequery (); return an int type, returns the number of successful rows for update and delete

A brief talk on Hibernate--hibernate inquiry (query Language) (GO)

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.