@@ -1091,6 +1091,99 @@ The order by expression with index is supported:
10911091 .orderBy(c - > c. asc(2 ))
10921092 .fetch();
10931093
1094+ Derived Table expression (Entityql, NativeSql)
1095+ ----------------------------------------------
1096+
1097+ We support subqueries using derived tables.
1098+ However, an entity class corresponding to the derived table is required.
1099+
1100+ Define the entity class corresponding to the derived table as follows:
1101+
1102+ .. code-block :: java
1103+
1104+ @Entity (metamodel = @Metamodel )
1105+ public class NameAndAmount {
1106+ private String name;
1107+ private Integer amount;
1108+
1109+ public NameAndAmount () {}
1110+
1111+ public NameAndAmount (String accounting , BigDecimal bigDecimal ) {
1112+ this . name = accounting;
1113+ this . amount = bigDecimal. intValue();
1114+ }
1115+
1116+ public String getName () {
1117+ return name;
1118+ }
1119+
1120+ public void setName (String name ) {
1121+ this . name = name;
1122+ }
1123+
1124+ public Integer getAmount () {
1125+ return amount;
1126+ }
1127+
1128+ public void setAmount (Integer amount ) {
1129+ this . amount = amount;
1130+ }
1131+
1132+ @Override
1133+ public boolean equals (Object o ) {
1134+ if (this == o) return true ;
1135+ if (o == null || getClass() != o. getClass()) return false ;
1136+ NameAndAmount that = (NameAndAmount ) o;
1137+ return Objects . equals(name, that. name) && Objects . equals(amount, that. amount);
1138+ }
1139+
1140+ @Override
1141+ public int hashCode () {
1142+ return Objects . hash(name, amount);
1143+ }
1144+ }
1145+
1146+
1147+ A subquery using a derived table can be written as follows.
1148+
1149+ .. code-block :: java
1150+
1151+ Department_ d = new Department_ ();
1152+ Employee_ e = new Employee_ ();
1153+ NameAndAmount_ t = new NameAndAmount_ ();
1154+
1155+ SetOperand<?> subquery =
1156+ nativeSql
1157+ .from(e)
1158+ .innerJoin(d, c - > c. eq(e. departmentId, d. departmentId))
1159+ .groupBy(d. departmentName)
1160+ .select(d. departmentName, Expressions . sum(e. salary));
1161+
1162+ List<NameAndAmount > list =
1163+ entityql. from(t, subquery). orderBy(c - > c. asc(t. name)). fetch();
1164+
1165+ The above query issues the following SQL statement:
1166+
1167+ .. code-block :: sql
1168+
1169+ select
1170+ t0_.NAME,
1171+ t0_.AMOUNT
1172+ from
1173+ (
1174+ select
1175+ t2_.DEPARTMENT_NAME AS NAME,
1176+ sum(t1_.SALARY) AS AMOUNT
1177+ from
1178+ EMPLOYEE t1_
1179+ inner join
1180+ DEPARTMENT t2_ on (t1_.DEPARTMENT_ID = t2_.DEPARTMENT_ID)
1181+ group by
1182+ t2_.DEPARTMENT_NAME
1183+ ) t0_
1184+ order by
1185+ t0_.NAME asc
1186+
10941187 Delete statement
10951188============================
10961189
0 commit comments