如何在Jooq两次与同一张桌子一起加入?

发布于 2025-02-11 18:16:48 字数 859 浏览 0 评论 0 原文

我有与另一个有一对一关系的表,但是其中有两个,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
        }

如何做得更好?

I have table which have one-to-one relation with another, but there are two of them, src and dst, how to join it right? I did something like that, but not sure this is best practce:

 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
        }

How to do it better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2025-02-18 18:16:48

最简单的方法(从JOOQ 3.17和投影表表达为 selectfield )的能力就是仅投影表本身,例如:

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

在您的特殊情况下,您甚至可以通过使用 nofollow noreferrer

// 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) ->
       // ...
   }

The easiest way (starting from jOOQ 3.17 and the ability of projecting table expressions as SelectField) is to just project the tables themselves, e.g.:

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

In your particular case, you can even simplify things further by using 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
       // ...
   }

Or even, without the it destructuring:

// 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) ->
       // ...
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文