spring-boot - 如何在 jOOQ 中两次加入同一张表?

标签 spring-boot kotlin jooq

我有一个与另一个表有一对一关系的表,但是有两个表,src 和 dst,如何加入它对吗?我做了类似的事情,但不确定这是最佳做法:

 val route = tbl.`as`("route")
 val srcPlace = Tables.PLACE.`as`("srcPlace")
 val dstPlace = Tables.PLACE.`as`("dstPlace")

 val records = dsl
        .select(route.asterisk())
        .select(srcPlace.asterisk())
        .select(dstPlace.asterisk())
        .from(route)
        .join(srcPlace).on(route.SRC_ID.eq(srcPlace.ID))
        .join(dstPlace).on(route.DST_ID.eq(dstPlace.ID))
        .limit(pageable.pageSize)
        .offset(pageable.offset)
        .fetch {
            val r = it.into(route).into(RouteEntity::class.java)
            val sP = it.into(srcPlace).into(PlaceEntity::class.java)
            val dP = it.into(dstPlace).into(PlateEntity::class.java)
            r.srcPlace = sP
            r.dstPlace = dP
            r
        }

如何做得更好?

最佳答案

最简单的方法(从 jOOQ 3.17 和 the ability of projecting table expressions as SelectField 开始)是只投影表本身,例如:

dsl.select(route, srcPlace, dstPlace)
   . // query
   .fetch {
       val (r, sP, dP) = it
       // ...
   }

在您的特定情况下,您甚至可以使用 implicit joins 进一步简化事情

// Assuming you named your foreign keys "src" and "dst"
dsl.select(route, route.src, route.dst)
   .from(route)
   .limit(pageable.pageSize)
   .offset(pageable.offset)
   .fetch {
       val (r, sP, dP) = it
       // ...
   }

甚至,没有 it 解构:

// Assuming you named your foreign keys "src" and "dst"
dsl.select(route, route.src, route.dst)
   .from(route)
   .limit(pageable.pageSize)
   .offset(pageable.offset)
   .fetch { (r, sP, dP) ->
       // ...
   }

关于spring-boot - 如何在 jOOQ 中两次加入同一张表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72812509/

相关文章:

java - 如何将 Spring ObjectProvider 与多个 bean 定义一起使用

kotlin - 在 kotlin 公开模型中,排序如何与限制一起工作?

java - Spring Boot 将请求放入队列并等待其他请求先完成

java - 如何在整个 Spring Boot 项目中将所有字符串变量设置为 upperCase()

android - 即使 java gradle 工具链设置为 jdk 11,Gradle 也使用 Java 1.8 构建 android 项目

java - 如何在jOOQ中表示连接运算符(||)?

java - 将 PostgreSQL 查询转换为 DSLContext 的问题

postgresql - jOOQ:只获得一个结果而不是所有现有条目

postgresql - 错误 : operator does not exist: timestamp without time zone >= boolean Hint: No operator matches the given name and argument type(s)

android - 如何修复由 : java. 引起的 lang.ClassNotFoundException:找不到类 "androidx.browser.customtabs.CustomTabsIntent$Builder"