Hibernate-based Hibernate query language (hql) II

Source: Internet
Author: User
Hibernate-based Hibernate query language (hql)
Hql
Keywords in hql are case-insensitive, but attributes and class names are case-sensitive.
1. Simple attribute Query [important]
* For a single attribute query, a list of returned result set attributes is returned. The element type is consistent with the corresponding attribute type in the object class.
* When querying multiple attributes, the returned collection element is an array of objects. The type of the array element is the same as that of the corresponding attribute in the object class.
The length of the array depends on the number of attributes in the SELECT statement.
* If you think that the returned array is not object-oriented enough, you can use hql to dynamically instantiate the student object.
See simplepropertyquerytest. java.
2. Object Query [important]
* N + 1 problem. By default, query. iterate queries can cause n + 1 problems.
The so-called n + 1 refers to the issue of N + 1 SQL statement during the query.
1: first, issue an SQL statement to query the Object ID list.
N: query data in the cache based on the ID list. If no matching data exists in the cache, the corresponding SQL statement is sent based on the ID.
* What is the difference between list and iterate?
* List issues an SQL statement each time. list puts data into the cache instead of using the data in the cache.
* Iterate: by default, iterate uses cached data. However, if no data exists in the cache, the problem of N + 1 may occur.
See simpleobjectquerytest1.java/simpleobjectquerytest2.java
3. Conditional Query [important]
* Parameters can be passed through String concatenation.
* Can be used? To pass parameters (index starts from 0)
* Parameters can be passed using the parameter name.
* If multiple parameters are passed, you can use the setparamterlist method.
* Database functions can be used in hql, such as date_format.
See simpleconditionquerytest. Java
4. hibernate also supports direct SQL queries.
See sqlquerytest. java.
5. External name query
* Use the <query> label in the ing file to define hql
* In Program Use the session. getnamedquery () method to obtain the hql query string.
See student. HBM. xml and namequerytest. java.
6. query Filters
* Define filter parameters in the ing File
* Use these parameters in class ing
* Enable the filter in the program
See student. HBM. xml and filterquerytest. java.
7. Paging Query [important]
* Setfirstresult (), starting from 0
* Setmaxresults: the number of data entries displayed on each page.
See pagequerytest. java.
8. Object navigation query, which is used in hql for navigation [important]
See objectnavquerytest. java.
9. Connection Query [important]
* Internal connection
* External connection (left connection/right connection)
See joinquerytest. java.
10. Statistical Query [important]
See statquerytest. java.
11. DML-style operations (use as little as possible because they are not synchronized with the cache)
See dmlquerytest. java.




Hql

MSO-Hansi-font-family: "Times New Roman" "> database query and retrieval is hibernate "Times New Roman" ">. Compared with other ORM "Times New Roman"> implementation, hibernate "Times New Roman"> provides a flexible and diverse query mechanism.

hibernate language query ( hql MSO-Hansi-font-family: "Times New Roman" "> ), it is a fully Object-Oriented Query Language with powerful query functions and features such as polymorphism and association. hibernate officially recommended hql MSO-Hansi-font-family: "Times New Roman">.

hql is a query object model, the table is not directly queried. hql MSO-Hansi-font-family: "Times New Roman" "> object-oriented generation SQL "Times New Roman" ">, replace tables and data columns with classes and attributes. It supports polymorphism, supports various associations, and reduces SQL "Times New Roman"> redundancy.

HqlSupports all relational database operations, including connection, projection, aggregation, sorting, subquery, andSQLFunction.

HqlSimple Example:

Query the user name with"JAll users starting:

Query query = session. createquery("Form user where user. name like 'J %'");

List users = query. List ();

Example of complex points:UserAndGroupTo findAdminAll users in the group.

Query query = session. createquery ("from user
User where user. Group. Name = 'admin '");

If you use a traditionalSQLThe query statement is as follows:

Select User. userid as userid, user. Name
Name, user. groupid as groupid, user. idcardid 
As idcardidForm tbl_user
User, tbl_group group where (group. groupname = 'admin' and
User. groupid = group. groupid)

The following describes how to experiment with hql:Take the two entity classes of students and classes as an example:

Public class student { Private int ID; Private string name; Private date createtime; Private classes; Public student (){} Public student (int id, string name ){ This. ID = ID; This. Name = Name; } Public int GETID (){ Return ID; } Public void setid (int id ){ This. ID = ID; } Public String getname (){ Return name; } Public void setname (string name ){ This. Name = Name; } Public classes getclasses (){ Return classes; } Public void setclasses (Classes classes ){ This. classes = classes; } Public date getcreatetime (){ Return createtime; } Public void setcreatetime (date createtime ){ This. createtime = createtime; } Public class classes {
Private int ID;
Private string name;
Private set students;
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public set getstudents (){
Return students;
}
Public void setstudents (set students ){
This. Students = students;
}
} The ing files of the two object classes are: <Class name = "com. bjsxt. hibernate. Student" table = "t_student">
<ID name = "ID">
<Generator class = "native"/>
</ID>
<Property name = "name"/>
<Property name = "createtime"/>
<Subtitle-to-one name = "classes" column = "classesid"/>
<Filter Name = "filtertest" condition = "ID & lt;: myid"/>
</Class>
<Query name = "searchstudents">
<! [CDATA [
Select s from student s where S. ID <?
]>
</Query>
<Filter-Def name = "filtertest">
<Filter-Param name = "myid" type = "integer"/>
</Filter-Def>
</Hibernate-mapping> and: <Class name = "classes" table = "t_classes">
<ID name = "ID">
<Generator class = "native"/>
</ID>
<Property name = "name"/>
<Set name = "Students" inverse = "true" cascade = "all">
<Key column = "classesid"/>
<One-to-learn class = "student"/>
</Set>
</Class>
</Hibernate-mapping> The Hibernate configuration file is: <Session-factory>
<Property name = "hibernate. Connection. url"> JDBC: mysql: // localhost/hibernate_hql </property>
<Property name = "hibernate. Connection. driver_class"> com. MySQL. JDBC. Driver </property>
<Property name = "hibernate. Connection. username"> root </property>
<Property name = "hibernate. Connection. Password"> bjsxt </property>
<Property name = "hibernate. dialect"> org. hibernate. dialect. mysqldialect </property>
<Property name = "hibernate. show_ SQL"> true </property>
<Mapping Resource = "com/bjsxt/hibernate/classes. HBM. xml"/>
<Mapping Resource = "com/bjsxt/hibernate/student. HBM. xml"/>
</Session-factory>
</Hibernate-configuration> For details, see the instance.

 









































































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.