Вы находитесь на странице: 1из 58

Query Language (JPQL)


JPQL

(Path Expression)
SELECT
SELECT

SELECT
FROM.


(Joins)
Inner JOIN collection-valued
Inner JOIN single-valued
JOIN
Joint WHERE
Multiple JOIN
Outer JOIN
Fetch JOIN
WHERE




BETWEEN
LIKE

IN

EXISTS
ANY, ALL, SOME



ORDER BY


Group BY
Having
Update
Delete

JPQL

JPQL SQL
JPQL ,
. ,
, SQL
JPQL,
SQL JPQL
, JPQL
SQL

JPQL





SQL

SQL,

4 :
Select
Aggregate
Update
Delete
,
persistence unit.

@Entity(name=xxx)

8

,
:
persistent
()
,
()
,
:

(Path Expression)


/
(.)
,
, :
(e.name)
single-valued (e.department)
collection-valued (e.managers)

e.department.name

10

11

SELECT


:
select_statement :: = select_clause
from_clause
[where_clause]
[groupby_clause]
[having_clause]
[orderby_clause]


JPQL

12

. SELECT

, :
SELECT e FROM Employee e

0 Employee
SQL:
SELECT id, name, salary, manager_id, dept_id, address_id
FROM emp


SQL

13

SELECT

SELECT :
SELECT d FROM
Department d

SELECT d.name FROM Department d


Single-valued SELECT e.department FROM
Employee e

:
SELECT d.employees FROM Department d

DISTINCT :
SELECT DISTINCT e.department FROM Employee e

14

SELECT


SELECT:
SELECT e.name, e.salary FROM Employee e

,


projection

15

projection
:
SELECT NEW example.EmployeeDetails(e.name, e.salary,
e.department.name) FROM Employee e


example.EmployeeDetails
example.EmployeeDetails

- JPA


DTO

16

SELECT

JPA



Query Processor ,
(cast)
:
SELECT p FROM Project p WHERE p.employees IS NOT EMPTY

Project, QualityProject
DesignProject
17

FROM.


range variable declaration

FROM:
SELECT d.employees FROM Department d

:
<entity_name> [AS] <identifier>

(path)

SELECT p FROM Employee e JOIN e.phones p

18

(Joins)

Join ,

Join :

FROM
JOIN
-

Join JPQL SQL JOIN

Inner JOIN ,

Outer JOIN Inner JOIN +
(left),
19

Inner JOIN collection-valued

:
[INNER] JOIN <path_expression> [AS] <identifier>
SELECT p FROM Employee e JOIN e.phones p

Employee Phone phones


(join query),
Phone, ,
Phone
JOIN ( !)
, JOIN
SQL:
SELECT p.id, p.phone_num, p.type, p.emp_id FROM emp e, phone p WHERE
e.id = p.emp_id
20

Inner JOIN single-valued

SELECT d FROM Employee e JOIN e.department d

Employee Department
department
:
SELECT e.department FROM Employee e

JOIN single-valued
OUTER JOIN

21

JOIN

JOIN,
(path)
SELECT DISTINCT e.department FROM Project p JOIN p.employees e WHERE
p.name = 'Release1' AND e.address.state = 'CA'

4 JOIN:
SELECT DISTINCT d FROM Project p JOIN p.employees e JOIN e.department d
JOIN e.address a WHERE p.name = 'Release1' AND a.state = 'CA'

5 JOIN SQL:
SELECT DISTINCT d.id, d.name
FROM project p, emp_projects ep, emp e, dept d, address a
WHERE p.id = ep.project_id AND ep.emp_id = e.id AND e.dept_id = d.id AND
e.address_id = a.id AND p.name = 'Release1' AND a.state = 'CA'

,

22

Joint WHERE


WHERE:
SELECT DISTINCT d FROM Department d, Employee e
WHERE d = e.department

, JOIN

:
SELECT d, m FROM Department d, Employee m
WHERE d = m.department AND m.manager IS NOT EMPTY

23

Multiple JOIN

JOIN

SELECT DISTINCT p
FROM Department d JOIN d.employees e JOIN e.projects p

, ,

,
,
(d, e, p)

24

Outer JOIN

,

:
LEFT [OUTER] JOIN <path_expression> [AS] <identifier>
SELECT e, d FROM Employee e LEFT JOIN e.department d

25

Fetch JOIN

(eager load) ,
, lazy
SELECT e FROM Employee e JOIN FETCH e.address

! JOIN FETCH

,
(pre-fetch)
SELECT e, a FROM Employee e JOIN
e.address a

FETCH JOIN collection-valued

26

WHERE

,

where_clause ::= WHERE conditional_expression

JPQL ,


SQL

27

,
WHERE BNF
conditional_expression ::= conditional_term | conditional_expression OR conditional_term
conditional_term ::= conditional_factor | conditional_term AND conditional_factor
conditional_factor ::= [ NOT ] conditional_primary
conditional_primary ::= simple_cond_expression | (conditional_expression)
simple_cond_expression ::=
comparison_expression |
between_expression |
like_expression |
in_expression |
null_comparison_expression |
empty_collection_comparison_expression |
collection_member_expression |
exists_expression
28

:
BETWEEN
LIKE

IN

EXISTS
ANY, ALL, SOME

29

:


SELECT e FROM Employee e WHERE e.salary > ?1
SELECT e FROM Employee e WHERE e.salary > :sal

30

BETWEEN

BETWEEN ,
,
SELECT e FROM Employee e WHERE e.salary BETWEEN 40000 AND 45000

:
Numeric
String
Date
BETWEEN . NOT BETWEEN

31

LIKE

LIKE

wildcards :
_

SELECT d FROM Department d WHERE d.name LIKE '__Eng%'

, _ %,
ESCAPE, ,
_ % ,
wildcard:
SELECT d FROM Department d WHERE d.name LIKE 'QA\_%' ESCAPE '\'
32

WHERE HAVING

SELECT
,
SELECT e FROM Employee e
WHERE e.salary = (SELECT MAX(e.salary) FROM Employee e)


( ) (
).

33

:
SELECT e FROM Employee e
WHERE EXISTS (SELECT p FROM Phone p WHERE p.employee = e AND p.type
= 'Cell')

,

. ,
joins inline view

34


JOIN:
SELECT e FROM Employee e
WHERE EXISTS (SELECT p FROM e.phones p WHERE p.type = 'Cell)

SQL:
SELECT e.id, e.name, e.salary, e.manager_id, e.dept_id, e.address_id
FROM emp e WHERE EXISTS
(SELECT 1 FROM phone p WHERE p.emp_id = e.id AND p.type = 'Cell')

JPQL
SELECT, p ,
SQL
35

IN

IN ,
single-valued
SELECT e FROM Employee e WHERE e.address.state IN ('NY', 'CA')
SELECT e FROM Employee e
WHERE e.department IN (SELECT DISTINCT d FROM Department d JOIN
d.employees de JOIN de.projects p WHERE p.name LIKE 'QA%')

IN (NOT):
SELECT p FROM Phone p WHERE p.type NOT IN ('Office', 'Home')

36

IS EMPTY IS
NULL
IS EMPTY (IS NOT EMPTY)
( ) collection-valued
SELECT e FROM Employee e WHERE e.directs IS NOT EMPTY

SQL IS EMPTY :
SELECT m FROM Employee m WHERE (SELECT COUNT(e) FROM Employee e
WHERE e.manager = m) > 0

37

MEMBER OF (NOT MEMBER OF) ,


collectionvalued :
SELECT e FROM Employee e WHERE :project MEMBER OF e.projects

:
SELECT e FROM Employee e
WHERE :project IN (SELECT p FROM e.projects p)

38

EXISTS

EXISTS true,

EXISTS NOT:
SELECT e
FROM Employee e
WHERE NOT EXISTS (SELECT p
FROM e.phones p
WHERE p.type = 'Cell')

39

ANY, ALL, SOME

ALL, ANY, SOME



ALL ,

ANY ( SOME) ,

=, <, <=, >, >=, <>

SELECT e
FROM Employee e
WHERE e.salary > ALL (SELECT m.salary
FROM Manager m
WHERE m.department = e.department)
40


WHERE HAVING
ABS(number) The ABS function returns the unsigned version of the
number argument. The result type is the same as the argument type
(integer, float, or double).
CONCAT(string1, string2) The CONCAT function returns a new string
that is the concatenation of its arguments, string1 and string2.
CURRENT_DATE The CURRENT_DATE function returns the current date
as defined by the database server.
CURRENT_TIME The CURRENT_TIME function returns the current time
as defined by the database server.
CURRENT_TIMESTAMP The CURRENT_TIMESTAMP function returns the
current timestamp as defined by the database server.

41

LENGTH(string) The LENGTH function returns the number of characters


in the string argument.
LOCATE(string1, string2 [, start]) The LOCATE function returns the
position of string2 in string1, optionally starting at the position indicated by
start. The result is zero if the string cannot be found.
LOWER(string) The LOWER function returns the lowercase form of the
string argument.
SIZE(collection) The SIZE function returns the number of elements in
the collection, or zero if the collection is empty.
SIZE
SELECT d FROM Department d WHERE SIZE(d.employees) = 2
SELECT d FROM Department dWHERE (SELECT COUNT(e)
d.employees e) = 2

FROM

42

MOD(number1, number2) The MOD function returns the modulus of


numeric arguments number1 and number2 as an integer.
SQRT(number) The SQRT function returns the square root of the
number argument as a double.
SUBSTRING(string, start, end) The SUBSTRING function returns a
portion of the input string, starting at the index indicated by start up to
length characters. String indexes are measured starting from one.
UPPER(string) The UPPER function returns the uppercase form of the
string argument.
TRIM([[LEADING|TRAILING|BOTH] [char] FROM] string) The
TRIM function removes leading and/or trailing characters from a string. If
the optional LEADING, TRAILING, or BOTH keyword is not used, then both
leading and trailing characters are removed. The default trim character is the
space character.

43

ORDER BY

, ,
:


single-valued
AS DESC
SELECT e
FROM Employee e
ORDER BY e.name DESC
SELECT e
FROM Employee e JOIN e.department d
ORDER BY d.name, e.name DESC
44

ORDER BY

SELECT ,
ORDER BY ,
SELECT:
SELECT e.name FROM Employee e ORDER BY e.salary DESC

e.salary SELECT,

45

,
,
()
SELECT AVG(e.salary) FROM Employee e
SELECT d.name, AVG(e.salary) FROM Department d JOIN d.employees e
GROUP BY d.name
SELECT d.name, AVG(e.salary) FROM Department d JOIN d.employees e
WHERE e.directs IS EMPTY GROUP BY d.name
SELECT d.name, AVG(e.salary) FROM Department d JOIN d.employees e
WHERE e.directs IS EMPTY GROUP BY d.name
HAVING AVG(e.salary) > 50000
46

5 ,
SELECT:
AVG
COUNT
MAX
MIN
SUM

47

AVG

,


- double

48

COUNT


, :

Single-valued

Long
SELECT e, COUNT(p) FROM Employee e JOIN e.phones p GROUP BY e

49

MAX

,


- double

50

MIN

,


- double

51

SUM

52

Group BY

GROUP BY ,


, :

Single-valued
!
SELECT GROUP BY.

:
SELECT d.name, COUNT(e), AVG(e.salary) FROM Department d JOIN
d.employees e GROUP BY d.name
53

Group BY


SELECT d.name, e.salary, COUNT(p) FROM Department d JOIN d.employees e
JOIN e.projects p GROUP BY d.name, e.salary

GROUP BY,

SELECT COUNT(e), AVG(e.salary) FROM Employee e

54

Having

HAVING ,
,
,

HAVING
SELECT e, COUNT(p)
FROM Employee e JOIN e.projects p
GROUP BY e
HAVING COUNT(p) >= 2

55

Update

UPDATE <entity name> [[AS] <identification variable>]


SET <update_statement> {, <update_statement>}*
[WHERE <conditional_expression>]


<update_statement> ( )

Single-valued
:



single-valued

56

Update

UPDATE Employee e SET e.salary = 60000 WHERE e.salary = 55000


:
UPDATE Employee e SET e.salary = e.salary + 5000
WHERE EXISTS (SELECT p FROM e.projects p WHERE p.name = 'Release2')

UPDATE
:
UPDATE Phone p
SET p.number = CONCAT('288', SUBSTRING(p.number, LOCATE(p.number, '),
4)), p.type = 'Business
WHERE p.employee.address.city = 'Ottawa' AND
p.type = 'Office'
57

Delete

DELETE FROM <entity name> [[AS] <identification variable>]


[WHERE <condition>]


Delete
(cascade rule)


DELETE FROM Employee e
WHERE e.department IS NULL

58