Hibernate query statement - HQL

xiaoxiao2021-03-06  116

1 .from

1.1 single table query

From eg.cat as cat. Among them, CAT is just an alias. In order to write simple when using other child statements.

1.2 multi-table query

From eg.cat, eg.dog from eg.cat as cat, eg.dog as dog 2 join related (inner) Join Left (Outer) Join Right (Outer) Join Full Join HQL also supports the following in SQL A small topic, the features on the upper, I haven't used it all the time. Since I said this, I want to talk about the usage of the few features, and I have a supplement to myself: Suppose there are two tables: Department, employee, listed below: Employee: ID Name DepNo 001 JPLATEAU 01 002 JONY 01 003 Camel 02 Department: ID Name 01 R & D Department 02 Marketing Department In Hibernate, we manipulate the object, so we are manipulated by department and employee class category 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 (notice that the conditional statement I have not used WHERE) So what is the execution result? 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 NULL NULL 02 marketing department of the R & D department {就 I have to have a second number of records in the second table, and there is no corresponding record in the first table, populate null} 3 case sensitive

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.

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

New Post(0)