Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 78405e1

Browse files
committed
Add a description of the Common Table Expression
1 parent a23bc80 commit 78405e1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

docs/query-dsl.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,54 @@ This generates:
10721072
order by
10731073
t0_.NAME asc
10741074
1075+
Common Table Expression
1076+
-----------------------
1077+
1078+
Common Table Expressions (CTEs) are supported.
1079+
To use a CTE, a corresponding entity class must be defined.
1080+
1081+
Define the entity class for the CTE as follows:
1082+
1083+
.. code-block:: java
1084+
1085+
@Entity(metamodel = @Metamodel)
1086+
public record AverageSalary(Salary salary) {}
1087+
1088+
A query using the CTE can be written as follows:
1089+
1090+
.. code-block:: java
1091+
1092+
var a = new AverageSalary_();
1093+
var e = new Employee_();
1094+
1095+
var cteQuery =
1096+
dsl.from(e)
1097+
.select(Expressions.avg(e.salary));
1098+
1099+
var list =
1100+
dsl.with(a, cteQuery)
1101+
.from(e)
1102+
.innerJoin(a, on -> on.ge(e.salary, a.salary))
1103+
.select(e.employeeId, e.employeeName, e.salary)
1104+
.fetch();
1105+
1106+
The above query generates the following SQL:
1107+
1108+
.. code-block:: sql
1109+
1110+
with AVERAGE_SALARY(SALARY) as (
1111+
select
1112+
avg(t0_.SALARY)
1113+
from
1114+
EMPLOYEE t0_
1115+
)
1116+
select
1117+
t0_.EMPLOYEE_ID,
1118+
t0_.EMPLOYEE_NAME,
1119+
t0_.SALARY from EMPLOYEE t0_
1120+
inner join
1121+
AVERAGE_SALARY t1_ on (t0_.SALARY >= t1_.SALARY)
1122+
10751123
Delete Statement
10761124
================
10771125

0 commit comments

Comments
 (0)