HQL data query for Hibernate, hibernatehql
1. HQL Introduction
HQL is an Object-Oriented Query Language. Compared with SQL query language, although it is similar in syntax, it parses data tables at runtime, but HQL does not operate on data tables as SQL does, database objects such as columns. The objects operated by HQL are classes, objects, and attributes. It also supports inheritance, polymorphism, and other features. Among the various retrieval methods provided by Hibernate, HQL is the most widely used.
2. Query interface
The Hibernate framework parses HQL statements and parses HQL query statements into corresponding SQL statements based on the ing configuration information to perform database query operations. This process depends on the Query interface. The Query instance is created by the createQuery () method of the Session. The method contains an HQL statement parameter. The list () method of the Query interface executes HQL Query. The result returned by the list () method is a List set, which stores persistent objects that meet the Query conditions.
3. Retrieve the object -- from clause
Set the one-way multi-to-one relationship between the two entities Commodity (Commodity) and Seller (Seller). Both of them have corresponding data. Use JUnit for testing.
3.1 output product name and seller name
Console output:
As you can see, the first statement is to query the commodity table to obtain all commodity information and output the first commodity name. Then, the second statement queries the merchant table based on the foreign key information to obtain the merchant name of the commodity. Next, output the name of the next item. Note that because the seller information corresponding to this item is also A clothing store, the corresponding query has been done and no new query is made, and directly use the existing query result A clothing store. When new seller information (B digital store) is not queried, the merchant table is also queried Based on the foreign key information.
This query process is due to a default lazy loading status. Hibernate does not query the data corresponding to the corresponding foreign key information by default. When necessary, make a temporary query according to the specific needs. This setting is also used to improve query efficiency and performance.
3.2 reference of persistent classes in the from clause
In the preceding HQL statement, from Commodity is used. Its full name is hbb. Commodity, but we only need to use its class name. When specifying a class in Java, you must specify a fully qualified name so that the java environment knows where to obtain the class. The reason why HQL statements can be omitted is that auto-import (auto-import) defaults. When parsing HQL statements, the persistence class is automatically imported according to the ing configuration information. Of course, you can use a fully qualified name in the from clause.
4. select the -- select clause
4.1 Return query results through Object []
The returned data type is not specified in the select clause. The default value is Object [].
Executed successfully. If only one attribute is queried and the Object [] is still used, an error is returned. If only one attribute is found, the list () method returns an Object instead of an Object array, in this case, make some adjustments.
You can also directly write List = query. list ();
4.2 return query results through List
4.3 return query results through Map
Note that the key value obtained by get is of the string type, so you need to add "". If the attribute uses an alias, you can change the sequence number to an alias.
5. Return query results through custom types
First, you need to define the corresponding constructor In the persistence class. The constructor parameter is the attribute information to be returned. Then, the constructor defined in the select clause is called. Return the query results of a custom type.
In the test class:
Note: After a user-defined constructor is added, you need to add a non-argument constructor to the object class. Because in HQL statements, when a query target (such as Seller) is specified ), hibernate will call the specified Constructor (such as new Seller () for encapsulation. However, if no constructor is specified, Hibernate will call the default constructor In the persistence class, for example, if no constructor is specified for the preceding Hql statement "from Seller", a non-argument constructor is required to create the corresponding object. Therefore, when a custom constructor is added, add a non-argument constructor.
6. Orderby clause
First, sort the merchant IDs in ascending order. If the IDs are the same, they are sorted in descending order by price. If the IDs are the same, they are sorted in ascending order.
7. Summary
HQL statements are case sensitive, especially persistence classes and their attributes. After we customize the constructor, we must add the default constructor. If you think it is useful to you, please like me or follow me. Thank you for watching.