最近搬到 JOOQ3.15.5并嘗試了該Multiset功能,但它正在拋出SQLSyntaxErrorException. 下面是我寫的查詢:
dslContext.select(
tableA.asterisk(),
multiset(
select(tableB.DELETED, tableB.VALUE)
.from(tableB)
.where(tableB.ORDER_ID.eq(tableA.ORDER_ID))
).as("bookingAdditions")
).from(tableA)
.where(tableA.BATCH_ID.greaterThan(batchId))
.fetchInto(BookingDto.class);
這是關系:
|tableA| 1 n |tableB|
| | --------------> | |
| | | |
-------- --------
(tableA) (tableB)
這是 JOOQ 生成的查詢:
set @t = @@group_concat_max_len; set @@group_concat_max_len = 4294967295; select `tablea`.*, (select coalesce(json_merge_preserve('[]', concat('[', group_concat(json_array(`v0`, `v1`) separator ','), ']')), json_array()) from (select `tableb`.`deleted` as `v0`, `tableb`.`value` as `v1` from `db_name`.`booking_additions` as `tableb` where `tableb`.`order_id` = `tablea`.`order_id`) as `t`) as `bookingadditions` from `db_name`.`booking` as `tablea` where `tablea`.`batch_id` > 0; set @@group_concat_max_len = @t;
以下是例外情況:
org.jooq.exception.DataAccessException: SQL [set @t = @@group_concat_max_len; set @@group_concat_max_len = 4294967295; select `tablea`.*, (select coalesce(json_merge_preserve('[]', concat('[', group_concat(json_array(`v0`, `v1`) separator ','), ']')), json_array()) from (select `tableb`.`deleted` as `v0`, `tableb`.`value` as `v1` from `db_name`.`booking_additions` as `tableb` where `tableb`.`order_id` = `tablea`.`order_id`) as `t`) as `bookingadditions` from `db_name`.`booking` as `tablea` where `tablea`.`batch_id` > ?; set @@group_concat_max_len = @t;]; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set @@group_concat_max_len = 4294967295; select `tablea`.*, (select coalesce(jso' at line 1
at org.jooq_3.15.5.MYSQL.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2988)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:639)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:349)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:295)
at org.jooq.impl.AbstractResultQuery.fetchLazyNonAutoClosing(AbstractResultQuery.java:316)
at org.jooq.impl.SelectImpl.fetchLazyNonAutoClosing(SelectImpl.java:2866)
at org.jooq.impl.ResultQueryTrait.collect(ResultQueryTrait.java:357)
at org.jooq.impl.ResultQueryTrait.fetchInto(ResultQueryTrait.java:1423)
at com.company.BookingDAO.fetchBookings(BookingDAO.java:118)
at
我正在使用Mysql: 5.7. 我究竟做錯了什么?任何提示?
uj5u.com熱心網友回復:
關于多重陳述
除了Bill Karwin所說的之外,您還可以為您的會話或您自己的服務器指定Settings.renderGroupConcatMaxLenSessionVariableto befalse并增加您的服務器@@group_concat_max_len變數。
jOOQ 在這里的默認設定是假設添加 JDBC 連接標志比更改服務器設定更容易,但這兩種方法都是可行的。
關于相關派生表
這個特定的模擬需要支持相關的派生表,MySQL 5.7 不支持,請參閱https://github.com/jOOQ/jOOQ/issues/12045。你不會得到一個MULTISET在 MySQL 5.7 上作業的相關運算式,但你可以撰寫一個幾乎等效的MULTISET_AGG運算式,如下所示:
dslContext.select(
tableA.asterisk(),
multisetAgg(tableB.DELETED, tableB.VALUE).as("bookingAdditions")
).from(tableA)
.join(tableB).on(tableB.ORDER_ID.eq(tableA.ORDER_ID))
.where(tableA.BATCH_ID.greaterThan(batchId))
// You can group by the primary key, or tableA.fields() if you don't have a PK
.groupBy(tableA.ID)
.fetchInto(BookingDto.class);
與MULTISET,MULTISET_AGG生成NULL值而不是空串列以防萬一您離開 join tableB,這對于 SQL 聚合函式來說是常見的。然后,您可以將其coalesce(multisetAgg(...), multiset(...))用作解決方法。
uj5u.com熱心網友回復:
當您GROUP_CONCAT()在查詢中使用時,jOOQ 會生成三個以分號分隔的 SQL 陳述句。不幸的是,MySQL 的默認行為是不允許在單個請求中進行多個查詢。
您必須更改 JDBC 連接選項以包含allowMultiQueries.
在此處閱讀更多相關資訊:https : //blog.jooq.org/mysqls-allowmultiqueries-flag-with-jdbc-and-jooq/
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/381380.html
