Hibernate Query Language: HQL (Select BLOG in JPLATEAU)

xiaoxiao2021-03-06  14

Hibernate Query Language: HQL HQL: Hibernate Quseding 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 characteristic.

AVG (...), SUM (...), Min (...), max (...) count (*) count (...), count (disc ...), Count (all. ..) Its usage and SQL basically the same Select Distinct Employee.name from Employee As Employe 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 operat =,> =, <=, <>,! =, Like logical operations and OR , NOT STRING CONCANATION || 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 only want to query The parameter problem description: Everyone knows that when the passing parameters in SQL are inquired, we usually write a lot of "?" "?" In the statement, in HQL, such as: List Mates = sess.find ("Select Employee.name from Employee As Employee" "Where Employee.name =?", Name, Hibernate.String; (Note: Using the Find method in the session, in H The Ibernate's API session is overloaded with a lot of find methods. It can meet your various forms of queries.) The upper side is a case where the parameters are introduced, and the type of parameters and define parameters are introduced. When multiple parameters, call Another Find method, the latter two parameters are the form of an array.

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

New Post(0)