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 clock is written as follows: from eg.cat, eg.dogfrom Eg.cat as cat, eg.dog as dog3.
Join Related (Inner) JoinRight (OUTER) JOINFULL JOINHQL The same for these features in SQL supports a small topic below, about those features on the top, I have not used it, since I said here, I want to think about it. Taking the usage of several features on the top, it is also a supplement to yourself: assuming there are two tables: department, employee, listed below: Employee: ID Name Depno001 JPLATEAU 01002 JONY 01003 Camel 02 department ( Department: ID Name01 R & D Department 02 Marketing Department In Hibernate, we manipulate the object, so we manipulate the departmental and employee 1). (Inner) JOINSELECT EMPLOYE.ID AS ID1, EMPLOYE.NAME AS Name1, Department .Id as id2, divartment.nameas name2 from Employee As Employee Join Department as department information on Employee.Depno = Department.id (notice that the conditional statement I have not used where where WHERE is used) So what is the execution? ID1 NAME1 ID2 NAME2 001 JPLATEAU 01 R & D No. 002 JONY 01 R & D 2) .left (outer) joinselect employee.ID as id1, employee.Name as name1, department.ID as id2, department.Nameas name2 from Employee as employee left join Department as department on employee.DepNo = department.ID then What should I do? ID1 NAME1 ID2 NAME2 001 JPLATEAU 01 R & D No. 002 JONY 01 R & D department 003 Camel null null {That is, when I have to have a record of the first table, I have no record in the second table, fill null} 3). Right (Outer) JOINSELECT EMPLOYE.ID AS ID1 Employee.name as name1, divartment.id as ID2, divartment.nameas name2 from Employee as Employee Right Join Department As Department on Employee.Depno = Department.ID So what is the execution result? ID1 NAME1 ID2 Name2
001 JPLATEAU 01 R & D No. 002 JONY 01 R & D Department NULL NULL 02 Marketing Department {That is, I have to have a second table at this time. The record is accurate, and NULL} 4 is filled when there is no record in the first table. 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.Nameas 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 in the end elements What is it used? It is expected to be given. This type of feature does not seem to support this type of characteristics in JDO. 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 "=" does not light the properties of the object, or the object can be compared, such as: select animal from com.test.animal as animal where aromal.name = DOG8.
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 Hibernate's API session Heavy loaded a lot of find methods, which can meet a variety of forms of queries) The situation is a parameter, which follows the type of parameters and definition parameters, when multiple parameters, call another Find method, Its 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.