Hibernate Summary 1. hibernate Summary
Use List, Map, and Class Object to customize the return type in hibernate.
When hibernate is used for query, the most commonly used query is by building hql. In the Query Process, in addition to frequently used object query methods, you can also query an attribute or a group of clustered results. In this case, we usually need to process the returned structure.
In general, we build hql and set the resultTransformer of the query to customize the type of the returned result, which is generally set to the map attribute, as shown in the following figure, each item of the query result is a map. :
Query query = session.createQuery("hql"); query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
However, with the development of hibernate, you can directly use the set query statements in hql, such as list and map. The following describes the query statements and query results when List and Map are used. First, the database data is as follows:
Mysql> select * from p_dictionary; + ----------------- + ---- + --------- + ------ + -------- + | dictionary_type | id | version | code | forbid | value | + --------------- + ---- + --------- + ------ + -------- + | COUNTY | 1 | 0 | 001 | Sichuan | COUNTY | 2 | 0 | 002 | Beijing | COUNTY | 3 | 0 | 001 | NULL | Sichuan | + --------------- + ---- + --------- + ------ + -------- + 3 rows in set (0.00 sec)
The following describes the query statements and query results using list and map respectively:
A. Use List
String query = "select new List (p. code, p. value) from Dictionary p "; List list = session. createQuery (query ). list (); System. out. println (list );
// Result: [001, Sichuan], [002, Beijing], [001, Sichuan]
The returned result of a list query is a List. The encapsulated objects in the List are divided into three types:
1. When you query all fields, such as "from object class", the objects encapsulated in the list are the object class, And all attributes are filled.
2. query only one field. By default, the Object is encapsulated in the list.
3. query two or more fields. By default, the list encapsulates Object [], and the length is consistent with the number of fields queried.
B. If Map is used, the alias (alias) is not specified first, and the result key is displayed in the query order, and 0, 1 is used to represent the key:
String query = "select new Map (p. code, p. value) from Dictionary p "; // fully object-oriented, using the new MapList list = session in java. createQuery (query ). list ();
// Result: [{1 = Sichuan, 0 = 001}, {1 = Beijing, 0 = 002}, {1 = Sichuan, 0 = 001}
When Map is used to specify alias, the key in the result is alias, which corresponds to the fields in the object class:
String query = "select new Map (p. code as code, p. value as value) from Dictionary p"; List list = session. createQuery (query). list ();
// Result: [{value = Sichuan, code = 001}, {value = Beijing, code = 002}, {value = Sichuan, code = 001}
If some are alias and some are not used, alias will be used as the key. If not, the serial number will be used instead. Where the serial number is the serial number in the query result.
String query = "select new Map (p. code as code, p. value) from Dictionary p "; List list = session. createQuery (query ). list (); // result: [{1 = Sichuan, code = 001}, {1 = Beijing, code = 002}, {1 = Sichuan, code = 001}]
C. Others
Bytes --------------------------------------------------------------------------------------------------
Select new List (p. name, p. address) from Person as p; // select stores the selected attributes in a List object select new ClassTest (p. name, p. address) from Person as p; // select encapsulate the selected attributes into objects, provided that ClassTest supports ClassTest (p. name, p. address) constructor :) select new Map (p. name as personName) from Person as p; // select name the selected expression as an alias. In combination with new Map (), the Map structure is selected, // use personName as the key and use the selected value as the value
Bytes --------------------------------------------------------------------------------------------------
D. One More Thing
I believe that the brothers who have used hibernate will be troubled by how to assemble the returned results into a VO after complicated multi-table queries. I keep worrying about this, but after reading the hibernate transform, I feel that this method is quite useful.
Suppose we now have a DTO whose attributes include the attributes of two tables. Now we need to convert the content obtained from the SQL statement (not an Hql statement) to a DTO object, the solution is as follows:
// SQL statement
String SQL = "select u. userName as userName, p. title as title, p. addTime as addTime from user as u, post as p where u. id = p. userId "// PostVO class, which contains the attributes of user information and post information, and sets the get \ set method. Make sure there is a default constructor in this class. Query q = getCurrentSession (). createSQLQuery (SQL). setResultTransformer (Transformers. aliasToBean (PostVO. class ));
You can see the source code of this part of hibernate. It is found that the AliasToBeanResultTransformer class is mainly used. through SQL queries, an array is returned, and then hibernate maps data tables, this will automatically help us set the corresponding field attribute, so the red part must be consistent with the attribute value in VO, or an error will be reported. You can rewrite this class if needed. For example, VOResultTransformer. Then change
setResultTransformer(new VOResultTransformer(PostVO.class));
Ps:
Reference blog: About the returned result set of hibernate pure SQL queries (multiple table associations are not written in hbm. xml)
Reference: hibernate-hql-createquery-list-type-cast-to-model-directly
// Add
// The package name com. example. DTO is used.
List <DTO> dtos = session. createQuery ("select new com. example. DTO (p. name, o. name) FROM Entity o"). list ();