看起来 Jooq 3.10 中的返回部分无法将值与名称内联
我尝试过这样的查询
val role = name("role").fields("role_id")
.`as`(select(ROLES.ROLE_ID)
.from(ROLES)
.where(ROLES.LEGACY_ID.eq(roleId)))
return dsl.with(role)
.insertInto(USER)
.columns(
USER.EMAIL,
USER.ROLE_ID)
.select(
select(
inline(email),
role.field("role_id", Int::class.java))
.from(role))
.onConflict(USER.EMAIL)
.doNothing()
.returning(USER.EMAIL, inline(roleId).as("role))
和
val role = name("role").fields("role_id")
.`as`(select(ROLES.ROLE_ID)
.from(ROLES)
.where(ROLES.LEGACY_ID.eq(roleId)))
return dsl.with(role)
.insertInto(USER)
.columns(
USER.EMAIL,
USER.ROLE_ID)
.select(
select(
inline(email),
role.field("role_id", Int::class.java))
.from(role))
.onConflict(USER.EMAIL)
.doNothing()
.returning(USER.EMAIL, inline(roleId))
第一种情况转换成这样的sql
select
'new_email@expedia.com',
role.role_id
from role
on conflict (email) do nothing
returning
auction.user.email,
role
我收到一条错误消息,提示没有角色字段
在第二种情况下它被转换为
select
'new_email@expedia.com',
role.role_id
from role
on conflict (email) do nothing
returning
auction.user.email,
2
并且我无法在获取阶段按名称引用内联列
最佳答案
确实,有一个已知且非常不幸的限制 InsertReturningStep.returning()
方法,只允许投影您插入的表中的列,不允许投影。
这个问题在 jOOQ 3.11 中通过新的 InsertReturningStep.returningResult()
得到了解决。通过问题的方法#1234 。由于向后兼容性的限制,原始方法没有进行改造。您现在可以执行以下操作:
.returningResult(USER.EMAIL, inline(roleId))
我认为在 jOOQ 3.11 中没有一种简单的方法可以解决这个问题,除非将整个查询转换为 plain SQL templating query
关于java - 返回部分中的内联名称会忽略 Jooq 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56987269/