Hibernate query language: HQL

zhaozj2021-02-12  149

Hibernate query language: HQL

HiBernate Qusery Language, if you are already familiar with it, you will find it very similar to SQL. However, you don't be confused by the face, HQL is an object-oriented (OO, looking at every object with life, they are so fresh). If you have some understanding of Java and SQL statements, then HQL is easier to make you feel like you, you can use the time on the bus to master it. Slowly in depth from several aspects: 1. Some sensitive, you know that Query is not sensitive to case, but in HQL (mentioned in front of it is OO), the object class's name and attributes are sensitive (in accordance with the Java programming syntax). Such as: select cat.name from cat as cat is the same, but: select cat.name from cat as cat and select cat.name from cat AS CAT is indeed different. 2. From statement: from eg.cat It is just simple to return all EG.cat, usually we will be Eg.cat at this time, because the rest of Query may be used (see the upper side for casement Examples of sensitiveity), such as: from eg.cat as cat here, AS can be omitted. The upper side is just a single table query, and the case of multi-table is written as follows: from eg.cat, eg.dog from eg.cat as cat, eg.dog as DOG 3.

JOIN Related (Inner) Join Left (Outer) Join Right (Outer) Join Full Join HQL The same for these features in SQL supports the following small topics, I haven't used the features on the upper side, I haven't used it today. Since it is said Here, I want to say the usage of several characteristics on the side, but also a supplement to yourself: Suppose there are two tables: department, employee, listed below: Employee: ID Name DepNo 001 JPLATEAU 01 002 JONY 01 003 Camel 02: ID Name 01 R & D Department 02 Marketing Department we manipulate in hibernate, so we manipulate departmental and employee 1). (Inner) Join Select Employee.id AS id1, employee.Name as name1, department.ID as id2, department.Name as name2 from Employee as employee join Department as department on employee.DepNo = department.ID (note that I use on a conditional statement did not use where) the results of executing what is it then? ID1 NAME1 ID2 NAME2 001 JPLATEAU 01 R & D No. 002 JONY 01 R & D 2) .left (outer) join select employee.ID as id1, employee.Name as name1, department.ID as id2, department.Name as name2 from Employee as employee left join Department as department on employee.DepNo = department. The ID is then what is the execution result? ID1 NAME1 ID2 NAME2

001 JPLATEAU 01 R & D system 002 JONY 01 R & D department 003 Camel null null {That is, when I have the first table of records, I have no record in the second table, fill null} 3). Right ( outer) join select employee.ID as id1, employee.Name as name1, department.ID as id2, department.Name as name2 from Employee as employee right join Department as department on employee.DepNo = department.ID then what should be the result of execution what? ID1 NAME1 ID2 NAME2 001 JPLATEAU 01 R & D No. 002 JONY 01 The R & D Department NULL NULL 02 Marketing Department {That is, when I have the second table of records, I have no record in the first table, fill null} 4. The SELECT statement is to determine which objects you have to return from the query or which objects. Write a few examples of it: select employee form Employee as employee select employee form Employee as employee where employee.Name like 'J%' select employee.Name form Employee as employee where employee.Name like 'J%' select employee.ID as id1 , employee.Name as name1, department.ID as id2, department.Name as name2 from Employee as employee right join Department as department on employee.DepNo = department.ID select elements (employee.Name) from Employee as employee (not understand elements What is it used to do? It is expected to be described), etc. 5. This type of feature does not seem to support this type of characteristics in JDO. AVG (...), SUM (...), min (...), max (...) count (*)

Count (...), count (distinct ...), Count (all ...) Its usage and SQL basically the same Select Distinct Employee.name from Employee As Employee Select Count (Distinct Employee.Name), Count (Employee) From Employee As Employee 6. Polymorphism (I don't know how to explain this time.) from com.test.animal as animal is not only available to all Animal instances, and you can get all animal subclasses (if we define a subclass CAT) A comparative extreme example from Java. Lang.Object AS O can get all persistent classes Example 7. define a condition where clause of the query, a few examples of it: from Employee as employee where employee.Name = 'Jplateau' from Employee as employee where employee.Name like 'J%' from Employee as employee where employee.Name like '% u '"=" in the where statement is not light to compare the properties of the object, or the object can be compared, such as: select animal from com.test.animal as animal where animal.name = DOG 8. Expressions can be used in HQL in the HQL in the SQL statement: Mathematical Operators , -, *, / binary comparison operatrs =,> =, <=, <>,! =, LIKE

Logical Operations and, OR, NOT

String concatenation ||

SQL SCALAR FUNCTIONS LIKE UPPER () AND Lower () Parentheses () Indicate Grouping

IN, Between, IS NULL

JDBC in parameters?

Named Parameters: Name,: start_date,: x1 (this should be another "?" workaround solution)

SQL LITERALS 'FOO', 69, '1970-01-01 10: 00: 01.0'

Java public static final constants eg.color.tabby Others don't have to be explained, here I just want to explain the parameters in the query: Everyone knows that when the parameters in SQL are delivered, we usually use preparedstatement, in the statement Write a lot of "?", You can also use this method in HQL, such as: list mats = sess.find ("SELECT EMPLOYE.NAME FROM EMPLOYEE As Employee" "Where Employee.name =?", Name, Hibernate.String); (Note: The Find method in the session is overloaded in the Hibernate's API Session, which can meet the situation in your various forms), in this case The type of parameter and definition parameter is introduced, and another Find method is called, and the latter two parameters are in the form of an array. There is another method to solve the problem of the upper side, and JDO has such a method, but there is a difference in the form of Hibernate, but their two bones are the same, such as: query = sess.createQuery ("SELECT EMPLOYEE .name from Employee as Employee WHERE Employee.Name =: Name "); Q.SetString (" Name "," JPLATEAU "); // When there are multiple parameters, it is here to ortholize iterator Employees = qiterate () ; 9. The Order statement and SQL statement have no difference, such as: select Employee.Name from Employee as Employee Employee.Name Like 'J%' Order by Employee.ID DESC (or ASC) 10.

Group By statement is also different from SQL statements, such as: select Employee.Name, Employee.Depno from Employee As Employee Group by Employee.Depno Select Foo.ID, AVG (Foo.Names), Max (ONDICES) Names))) "from eg.foo foo group by foo.id {NOTE: you may use the elements and indes, evening a select clause, eve databases with no subselects.} Who helped me explain the two sentences, thank you! 11. Sub-query Hibernate also supports child query, written a few examples: from eg.cat as fatcat where fatcat.weight> (SELECT AVG (Cat.Weight) From Eg.Domesticcat Cat) Section: In fact, HQL and SQL are very similar, When you write, you just want to think of the concept of the object, then you can write HQL with SQL's idea. Reference Resources: Hibernate Reference Manual Chapter 7 and Chapter 9 Study this manual. Yesterday, I saw the "Submarine Mobilization". I don't know when China's cartoon can "station"?

转载请注明原文地址:https://www.9cbs.com/read-6107.html

New Post(0)