Hibernate retrieval method (4)

Source: Internet
Author: User
Copyright statement: Reprinted with a hyperlink Article Source and author information and this statement
Http://aumy2008.blogbus.com/logs/14005572.html

IV, REPORT query

1Projection Query

Select C from customer C ......

Select C. Name, C. Age from customer C ......

(1) Dynamically instantiate the query results.

Select NewCom. customerrow (C. ID, C. Name, C. Age)From customer C ......

Note:CustomerrowThe class does not need to be a persistent class, so it does not need to create its object-link ing file, which is closely usedSelectThe link data queried by the statement is packagedJavaObject.

(2) Filter duplicate elements in the query results.

SelectDistinct C. NameCustomer C ......

2Use Aggregate functions

InHqlThe following Aggregate functions can be called in a query statement:

LCount (): Number of statistical records

LMin ()

LMax ()

LSum ()

LAVG (): Average Value

3, Grouping query (Group... (Having ...))

Select C. Name, count (c) from customer C group by C. Name

List ()Result:NArray 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]; IntegerCount= (Integer) pair [1];

}

4Optimized REPORT query performance

REPORT query features: it usually processes a large amount of data. Generally, it only involves reading data without modifying data.

WhenSelectWhen only some attributes of the persistence class are queried,HibernateThe returned query result is relational data (not occupyingSessionInstead of the persistence object (located inSessionCache ).

IfSelectStatement query can improve the performance of REPORT query, as long as the applicationProgramIf the data is not referenced, the memory occupied by the data will be released. WhenFromTypeHqlA large number of persistent objects remain inSessionAndSessionBixun is also responsible for synchronizing these objects to the database.

ForSelectStatement query, you can defineJavaBeanTo 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

1Dynamic query

HqlAndQBCCan complete many identical tasks, in contrast,HqlCan intuitively express complex query statements, whileQBCIt 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.HqlQuery Method. However, if the content of the query statement (also called dynamic query) can be specified only when the program is running ),QBCRatioHqlMore convenient.

QBEFuzzy match example:

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 indicates that case sensitivity is ignored when comparing strings. enablelike indicates the template object excludezeroes indicates that the numeric type attribute in the sample object is 0 , do not add it to the query statement. 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.QBESearch.

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...

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.