看起来 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


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!