Hibernate query related knowledge Time: 2009-10-04 07:14:25 Source: Internet Author: Unknown Clicks: 146 times First introduce the difference between get() and load() methods:
The difference between the get() method and the load() method mainly lies in the use of second-level cache.
The load() method will use the second-level cache, and the get() method will directly query the database if it is not found in the first-level cache, and will not search in the second-level cache.
get(): If you first introduce the difference between get() and load() methods in the database:
The difference between the get() method and the load() method mainly lies in the use of second-level cache.
The load() method will use the second-level cache, and the get() method will directly query the database if it is not found in the first-level cache, and will not search in the second-level cache.
get(): If there is no record in the database, it will return null. get() will return data anyway.
load(): If there is no record in the database, an exception will be thrown. If there is data, a proxy object will be returned.
Difference between list and iterator() methods: (N+1?)
When the list() method is executed, it directly runs the query statements required for the query results.
The iterator() method first executes the query to obtain the object ID, and then obtains the object to be queried based on each ID value.
Therefore: For queries using the list() method, only one SQL statement is usually executed, while for queries using the iterator() method, N+1 SQL statements may need to be executed (N is the number of records in the result set).
Result sets are processed differently:
The list() method will activate all result set objects at once, and it will initialize all result set objects based on the query results. If the result set is very large, it will occupy a lot of memory and even cause memory overflow.
The iterator() method does not initialize all objects at once during execution, but initializes objects based on access to the result set. You can control the number of objects in the cache during one access to avoid taking up too much cache and causing memory overflow.
HQL: HQL is an object-oriented query language. The operating objects of HQL are classes, instances, attributes, etc.
SQL: The operation objects of sql are data objects such as data tables and columns.
Hql is a completely object-oriented query language, so it can support features such as inheritance and multiple items.
HQL query relies on the Query class, and each Query instance corresponds to a query object.
The function of setting parameters, the Query interface is the real HQL query interface.
//Create a Query object
Query query = session.createQuery ("from Customer as c where c.name=:customerName and c.age=:customerAge");
//Dynamic binding parameters
query.setString("customerName","Tom");
query.setInteger("customerAge",21);
//Execute the query statement and return the results
List result = query.list();
HQL query steps:
1: Get the Hibernate Session object.
2: Write HQL statements.
3: Use the HQL statement as a parameter to call the createQuery method of Session to create a query object.
4: If the HQL statement contains parameters, call the setXXX() method of Query to assign values to the parameters.
5: Call the list and other methods of the Query object to traverse the query results.
Query also contains two methods:
setFirstResult(int firstResult): Set the record from which the returned result set starts.
setMaxResults(int maxResults): Set the number of results returned by this query.
Entity deletion and update.
Projection query: only query part of the attribute.
Querying an attribute returns a string
Querying two fields returns an array
Dynamically constructed query: mainly used for dozens of table queries;
To construct a new object, add a constructor;
When creating a new object, add the package name
Don't use count(*), use count(persistent object)
Grouping and sorting:
Order by clause can be ordered by asc or desc keywords
For example: form User u Order by u.name asc,u.age desc;
Group by clause and statistical query:
For example: String hql = "select count(u),u.age from User u group by u.age having count(u)>10";
List list = session.createQuery(hql).list();
Standard SQL aggregate functions can be used in HQL statements, such as: count(), sum(), max(), min(), age(), etc.
Connection query:
Inner join: inner join
Left outer join: left outer join
Right outer join:right outer join
Full join: full join (not commonly used)
Urgent outer join: left outer join fetch, left join
fetch: used to obtain connection data at one time, especially collection data. Reduce the number of interactions with the database.
left out join: Use an outer join to perform an outer join. All records in the left table and the corresponding record information of the order class will be displayed.
right out join: Just the opposite of left out join, right out join returns all records in the right table of HQL and the corresponding Customer object record information.
Four ways to obtain collection data:
1:Hibernate.initialize(user.getOrder());
2:user.getOrder().size();
3: Left and right urgent connection
4: Advanced filters
Criteria Queries: Use objects to perform object queries.
The main interfaces are: Criteria, Criterion, expression_r and Restrictions classes. Ability to support dynamic generation of SQL statements at runtime.
Conditional query steps:
1: Create a Criteria object through seesion's CreateCriteria() method
2: Set the query object, name refers to the attribute of the object
3: Add query conditions to the Criteria object
4: Execute list() query to return the results.
Conditional query is completed through three classes:
Criteria: represents a query.
Criterion: represents a query condition.
Restrictions: Tool class for generating query conditions.
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/jzaccp_xiaoxiong/archive/2009/12/31/5111135.aspx