The criteria query statement for hibernate is a more object-oriented approach to querying.
Criteriabuilder Criteriabuilder = Entitymanager.getcriteriabuilder (); Create a Entitymanager to create a criteriabuilder real column criteriaquery<t> criteriaquery = Criteriabuilder.createquery ( Entityclass);//criteriabuilder creates a createquery real column, Criteriaquery is given a generic type, and the generic parameter declares the type of result that criteriaquery returns at execution time.
Criteriaquery.select (Criteriaquery.from (Entityclass)); Set up a query expression on Criteriaquery
Return Findlist (Criteriaquery, First, count, filters, orders);//return result set
hibernate
String jpql =" Select COUNT (*) from agent agent where lower (agent.name) = lower (: name) ";//Create JPQL statement (it should be noted that the agent behind the from is an entity bean and not a data table.) )
long count = Entitymanager.createquery (JPQL, Long.class)// Get a createquery real column from Entitymanager, the first position is the JPQL statement, the second position is the reflection object, and the object is the type .setflushmode (flushmodetype.commit) of the return value. Setparameter ("name", name)//Refresh when the query executes. Set query parameters. Getsingleresult ();//Returns a single result
Through the comparison between the two statements can deepen the deep understanding of criteriaquery query statements, of course, the premise is to be familiar with a language.in the shop++ program,It is recommended to use JPQL statements when using generic queries, because implementing a simple query, for example, also createsEntitymanager then create a createquery real column. This virtually increases the code, and the Code execution efficiency is reduced a lot. Of course, if you want to do a slightly more complex query, such as dynamic query, you need to increase the number of conditions, sorting and so on the condition query can be used criteriaquery query statements.
shop++ Criteriaquery Database Query statements