Hibernate three query methods

xiaoxiao2021-03-05  27

(1) 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 SQL-92 query is not sensitive to case, but in HQL (previously mentioned it is OO), the name and attribute of the object class is sensitive to the object class (in accordance with the Java programming syntax).

The HQL clause is not related to the case, but the class name and attribute names appear must pay attention to the case. If: select cat.name from cat as cat and select cat.name from cat as cat is the same but: select cat.name from Cat Cat as cat and select catch .name from cat as cat is indeed different. 2. The strom statement is the simplest: from eg.cat it just returns all EG.cat instances, usually we will be eg.cat at this time, because the rest of Query may be used (see the size Example of writing sensitive), 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. Take the usage of the few features of the upper 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 00 Camel 02

Department (Department): ID Name 01 R & D Department 02 Marketing Department we manipulate in hibernate, so we manipulate departmental and employees

1). (Inner) JOINSELECT EMPLOYE.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 useless where where there is useless? 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 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.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 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 "=" 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.

GROUP BY statement is also different from SQL statements, such as: select Employee.name, Employee.Depno from Employee As Employee Group by Employee.DepnOSelect Foo.ID, AVG (Elements (foo.names)), Max (Foo.Names) ))) "from eg.foo foo group by foo.id {NOTE: you may use the elements and indices, evening data.} Who helped me explain the two sentences, thank you! 11. Sub-query Hibernate also supports subqueries, written several examples: from eg.cat as fatcat where fatcat.weight> (SELECT AVG (Cat.Weight) (2) Condition Query Criteria Query. Mathematical function

JDO

This type is currently not supported.

AVG (...), SUM (...), Min (...), max (...) count (*) count (...), count (disc ...), Count (all. ..)

Its usage and

SQL

basically the same

Select Distinct Employee.name from Employee As Employee Select Count (Distinct Employe.name), Count (Employee) from Employee As Employee 6

.

POLYMORPHISM

I don't know how to explain this time.

) from com.test.animal as animal

Not only getting all

Animal

Example, and you can get all

Animal

Subclasses (if we define a subclass

Cat

)

A comparative extreme example

From java.lang.object as o

Examples of all persistent classes can be obtained

Seduce

.

WHERE

Statement

Define the conditions for query statements, and take a few examples:

From Employee As Employee WHERE EMPLOYE.NAME = 'JPLATEAU'FROM Employee As Employee Where Employee.Name Like' J% 'from Employee As Employee Where Employee.Name Like'% U '

in

WHERE

"In the statement"

=

"Not only can 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 = DOG8

. expression

in

SQL

Most of the expressions in the statement are

HQL

You can use it in:

Mathematical Operators , -, *, / binary comparison operat =,> =, <=, <>,! =, 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 explain, here I just want to explain the parameters in the query:

Everyone knows

SQL

When the transmission parameters are inquired, we usually use

PreparedStatement

, Write a lot of "?" In the statement,

in

HQL

This method can also be used

,Such as:

List mats = sess.find ("SELECT EMPLOYE.NAME FROM EMPLOYEE As Employee" "Where Employee.Name =?", Name, hibernate.string; (

Description: Used above

Session

inner

Find

Method, in

Hibernate

of

API session

There are a lot of heavy loads.

Find

Method, it can meet a variety of forms of queries

)

The upper side is a parameter situation, which is the type of introduction parameter and defining parameters in this case, and call another one when it is multiple parameters.

Find

Method, it's last two

The parameters are all forms of arrays.

There is another way to solve the problem of the upper side.

JDO

There is also such a method, but

Hibernate

There is a difference in the form of expression, but they are in both bones.

The same, such as:

Query Q = Sess.createQuery ("Select Employee .Name from Employee As Employee Employee.Name =: Name"); Q.SetString ("Name", "JPLATEAU"); //

When there are multiple parameters, it is certainly a sense of one.

Iterator Employees = q.Itemate (); 9

.

ORDER

Statement

with

SQL

There is no difference in statement, such as:

Select Employee.name from Employee As Employee WHERE EMPLOYE.NAME LIKE 'J%' ORDER by Employee.ID DESC (

or

ASC) 10

.

GROUP BY

Statement

Similarly

SQL

There is no difference in statement, such as:

select employee.name, employee.DepNo from Employee as employee group by employee.DepNoselect foo.id, avg (elements (foo.names)), max (indices (foo.names)) from eg.Foo foo group by foo.id {NOTE: You May Use The elements and indices constructs inside a select clause, Even on databases with no subselects.} Who will explain the top two sentences, thank you!

11

. Subquerice

Hibernate

Similarly, subqueries, write a few examples:

From eg.cat as fatcat where fatcat.weight> (SELECT AVG (Cat.Weight) from eg.domesticcat cat) (2) Condition Query criteria query

Criteria criteria = OSSION.CREATECRITERIA (OWNER.CLASS); criteria.Add (Expression.eq ("age", new integer (100)))))); criteria.setfirstResult (2); // From the return result, the start 5 records of criteria.setmaxresults (5); list lc = criteria.list (); system.out.println ("conditional query); system.out.println (lc.size ());

(3) native SQL statement query

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

New Post(0)