Lhibernate a total of 5 query methods
? OID Data Query method
? HQL Data Query method
? QBC Data Query method
? Local SQL Query method
? Ogn Data Query method
OID Data Query method
L Premise: The OID of the object has been acquired
L Query: Based on OID, use the Get/load method to query the corresponding data
L Function: Use OID to get corresponding data
HQL Data Query method
L Premise: The data structure and conditions are known to be queried
L Query: Based on Query object, complete HQL statement queries, get query results
L Function: Query the corresponding data based on HQL grammar rules
QBC Data Query method
L Premise: The data structure and conditions are known to be queried
L Query: Based on Criteria object, complete object-oriented query, get query results
L Role: Query the corresponding data based on the object-oriented rules
Local SQL Query method
L Premise: The data structure and conditions are known to be queried
L Query: Based on SQL statements, complete the query, get query results
L Function: Query the corresponding data based on SQL syntax rules
Ogn Query method
L Premise: A Query Object (PO) has been obtained
L Query: Use retrieved objects (PO) to navigate to other data contained in it
L Role: Query all other object data associated with the loaded object
Hql Query method
Lhql Query method is query by HQL statement
LHQL (Hibernate query Language) is a hibernate-specific query statement that transforms SQL statements into an object's operational format based on an object-oriented pattern
Lhql Query Method steps
? Get Session Object
? writing HQL statements (satisfying HQL syntax rules)
? Initialize the Query object (created with session object, passing in the HQL parameter)
? Initialize the query parameter (if necessary)
Execute a query to return results (return a single piece of data or collection data)
hql--Simple Query
LHQL When writing query statements, if you only make a query for a table, and the query data is all data, you can use the object name in ORM to represent the table name, the query content can be omitted
? In the actual development, almost no such operation, SQL statement writing needs to consider the operational efficiency, usually add indexes for some properties, improve query performance
hql--chained format
LHQL write a lot of code, can be done through a single statement, this is the chain format of the HQL query
The chain format is far more powerful than the above, and is more powerful in HQL conditional queries and QBC conditional queries, but only in the form of changes that do not require too much attention.
hql--Get Data (return object)
Querying content: A single property or object
L Query Results: 0 to more than one data
L Use the list () provided by the query object to complete the data acquisition and get a list collection. A single piece of data in the list encapsulates the object that corresponds to that data, which can be any type
L Case:
?" From Teachermodel "
? Returns an object of the Teachermodel class
?“ Select Age from Studentmodel "
The object that returns the Age property of the Studentmodel class, the object type integer
hql--Get Data (return object)
hql--using aliases
LHQL writing, if the class name is too long, or the property name is too long, you can simplify the writing by means of an alias, and you can describe the result of the query as the object type
Alias usage rules are exactly the same as SQL, as can be omitted
L "Select Tm.nick from Teachermodel TM" (correct)
hql--fetching data (returning an array of objects)
L Query content: multiple attributes or objects
L Query Results: 0 to more than one data
L Use the list () provided by the query object to complete the data acquisition and get a list collection. An array of objects encapsulated in a single piece of data in the list that encapsulates all the query results in sequence
L Case:
?" Select Teachername,nick from Teachermodel "
? Returns the TeacherName and Nick Properties of the Teachermodel class
?“ Select Skill,age from Studentmodel "
? Returns the skill and age and attributes of the Studentmodel class
L query more than one data content can not be encapsulated into an object, so encapsulated into an array of objects, the number of data in the object array depends on the number of fields/properties to query at query, which can be mixed with the format of the object and property
L The first data queried in the above query is a property, and the second data is an object, and this format is allowed.
L If multiple data results of a query can be packaged as an object, it can be encapsulated using the projection technique, which is explained in detail later.
hql--Get Data (return object)
Query content: Unlimited
L Query Results: 0 to 1 data
L Use the Uniqueresult () provided by the query object to complete the data acquisition and get an object after getting it. The difference from list () is that this operation can only be used to obtain data from 0 to 1 of the query results, if the query results exceed 1, the program will throw the query result is not unique exception
L Case:
?" From Teachermodel where uuid = 1 "
? Returns the Teachermodel class with a UUID of 1 objects, up to one
The Luniqueresult method is typically used to perform query aggregation functions
Rules for using hql--aggregation functions
The L aggregation function is a built-in function that provides a quick statistical operation in the SQL language
Lhibernate compatible with the following aggregate functions in SQL statements
? Count ()
? min ()
? Max ()
? sum ()
? AVG ()
L Format:
?“ Select COUNT (UUID) from Teachermodel "
The result returned by the Lcount function must be received with the Long data
Other functions are selected based on the data type of the operation, for example, the age in the previous example needs to be received using an integer type
hql--Paging Query
LHQL the paging in the query is no longer done manually by the user, you can use the method provided by the query object to complete
? setfirstresult (int);
? setmaxresults (int);
L The above two method return values are the original Call object query object, so the chained format is supported
hql--condition Query
L conditional query is the most common operation in practical application, there is almost no condition to carry the whole table data query phenomenon
LHQL conditional queries are divided into three formats
? no parameter condition query
? fixed parameter query
? dynamic parameter Condition Query
hql--index format Dynamic binding parameters
L Use Set type name () to complete parameter passing (common) if you anticipate incoming parameter types
? Query all Java programmers over the age of 30
L Use the index format to pass parameters, the corresponding HQL statement parameter position change, set parameter code still need to maintain, not recommended to use.
When using parameter setting, parameters can only be assigned to the specified query property name, if the parameter's property is not a wrapper class for the base data type, then the entity needs to be used to assign the value
? Query all students of the teacher number 1
l Use the format of object traversal to complete the above operations
l Use the format of the pass-through object parameter
? Query all students of the teacher number 1
The Setentity method can complete the transfer of object parameters
The Setentity method requires that the passed in parameter must be a PO or do
hql--name format Dynamic binding parameters
l use indexed format matching parameters is not flexible enough, maintenance is difficult, HQL also supports the definition of temporary variable name specified parameters (recommended)
L Use the name and index can complete the corresponding function, transfer parameters when the three parameter assignment format is applicable (param, type, entity)
hql--conditional query (chained style)
L conditional query support chain style
Lquery the operation of the object if the returned result is of type query, then the operation supports chained style writing
hql--Projection mode Query
L General Query Settings query content
When querying a single property or object, the collection of objects is returned
Query the combined format of multiple properties or objects to return an array of objects
L GET the names and ages of all students
The function of the projection query is to encapsulate the data of the query
To encapsulate query results as objects
? encapsulate query results as a collection
• Encapsulating query results into objects using a construction method
Requires a corresponding construction method in the corresponding PO class.
? You can provide a variety of construction methods for the PO class to accommodate a wide variety of projections
The projection format is more flexible, if you define multiple construction methods (no parameter construction method)
hql--Multi-Table Association query
Hibernate Framework Learning Data query (HQL)