Hibernate retrieval method (4)-[hibernate
]
Copyright Notice
: During reprinting, please use hyperlinks to indicate the original source and author information of the article and this statement
Http://aumy2008.blogbus.com/logs/14005572.html
Iv. REPORT query
1
Projection Query
Select C from customer C ......
Select C. Name, C. Age from customer C ......
(
1
) Dynamically instantiate the query results.
Select New COM. customerrow (C. ID, C. Name, C. Age)
From customer C ......
Note:
Customerrow
The class does not need to be a persistent class, so it does not need to create its object-link ing file, which is closely used
Select
The link data queried by the statement is packaged
Java
Object.
(
2
) Filter duplicate elements in the query results.
Select distinct C. Name
Customer C ......
2
Use Aggregate functions
In
Hql
The following Aggregate functions can be called in a query statement:
L
Count ()
: Number of statistical records
L
Min ()
L
Max ()
L
Sum ()
L
AVG ()
: Average Value
3
, Grouping query (
Group... (Having ...)
)
Select C. Name, count (c) from customer C group by C. Name
List ()
Result:
N
Array elements, each object array corresponds to a record in the query results. For example
Iterator it = session. createqurey (
"Select C. Name, count (c) from customer C group by C. Name"
)
. List (). iterator ();
While
(It. hasnext ()){
Object [] pair = (object []) it. Next ();
String name = (string) pair [0];
Integer count
= (Integer) pair [1];
}
4
Optimized REPORT query performance
REPORT query features: it usually processes a large amount of data. Generally, it only involves reading data without modifying data.
When
Select
When only some attributes of the persistence class are queried,
Hibernate
The returned query result is relational data (not occupying
Session
Instead of the persistence object (located in
Session
Cache ).
If
Select
Statement query improves REPORT query performance. As long as the application does not reference the data, the memory occupied by the data is released. When
From
Type
Hql
A large number of persistent objects remain in
Session
And
Session
Bixun is also responsible for synchronizing these objects to the database.
For
Select
Statement query, you can define
JavaBean
To encapsulate the relational data in the query results, so that the application can still access the query results in an object-oriented manner.
5. Advanced query skills
1
Dynamic query
Hql
And
QBC
Can complete many identical tasks, in contrast,
Hql
Can intuitively express complex query statements, while
QBC
It is difficult to express complex query statements.
If the content of the query statement (also called static query) is specified before the program runs, it should be given priority.
Hql
Query Method. However, if the content of the query statement (also called dynamic query) can be specified only when the program is running ),
QBC
Ratio
Hql
More convenient.
QBE
Fuzzy match example:
Customer customer = ....
Example examplecustomer = example. Create (customer );
Examplecustomer. ignorecase (). enablelike (matchmode. Anywhere );
Examplecustomer. excludezeroes ();
Criteria = session. createcriteria (customer.
Class
). Add (examplecustomer );
List list = criteria. List ();
Note:
Ignorecase
Case sensitivity is ignored when comparing strings;
Enablelike
Fuzzy comparison is used for all string type attributes in the sample object;
Excludezeroes
Indicates that the numeric type attribute in the sample object is
0
. If a property is
Null
The query statement does not contain this condition.
You can specify both the customer query conditions and order query conditions in the query window.
QBE
Search.
Customer customer = ....
Example examplecustomer = example. Create (customer );
Examplecustomer. ignorecase (). enablelike (matchmode. Anywhere );
Examplecustomer. excludezeroes ();
Order order =...
Example exampleorder = example. Create (order );
Exampleorder. ignorecase (). enablelike (matchmode. Anywhere );
Exampleorder. excludezeroes ();
Criteria = session. createcriteria (customer.
Class
)
. Add (examplecustomer)
. Createcriteria (
"Orders"
)
. Add (exampleorder );
List list = criteria. List ();
To be continued...