JOOQ中有没有一种方法可以在不多次数据库调用的情况下提取大量记录?
在我们的网络应用程序中,我们有很多地方可以在一个复杂的表单/视图中更新多个表。在原始 SQL 中,我可能会从一堆表中选择一堆列,并编辑主表以及相关父/子表上的一条记录。
在 hibernate 中,我可能只是为主表拉取一个 JPA 实体,并让 hibernate 在填充视图时获取父/子关系。然后从我的视图拉回到实体并调用entitymanger .perist/merge。
在 JOOQ 中,我有很多选项,但看来您可以通过 selectFrom/fetch 拉取主记录,然后使用 fetchChild fetchParent 来拉取类型相关记录,如下所示...
LoadsRecord load = dslContext.selectFrom(LOADS)
.where(LOADS.ID.eq(id))
.fetchOne();
SafetyInspectionsRecord safetyInspection = load.fetchParent(Keys.LOADS__FK_SAFETY_INSPECTION);
因此,这样我就能够以类型安全的方式拉取相关记录。唯一烦人的是,每次调用 fetchParent 或 fetchDhild 时,我都必须运行另一个完整查询。有没有一种方法可以立即获取所有这些内容以避免多次往返数据库?
拥有像 LoadsRecord 这样的类用于 CRUD 屏幕真是太好了,它使更新数据库变得容易。
In our webapp we have a number of places where you would be updating a number of tables in one complex form/view. In raw SQL I would probably select a bunch of columns from a bunch of tables and edit that one record on the primary table as well as related parent/child tables.
In hibernate I would probably just pull a JPA entity for the main table and let hibernate fetch the parent/child relationships as I populate the view. And then later pull from my view back to the entity and call entitymanger .perist/merge.
In JOOQ I have a number of options but it appears you can pull a main record via selectFrom/fetch then use fetchChild fetchParent to pull typed related records like so...
LoadsRecord load = dslContext.selectFrom(LOADS)
.where(LOADS.ID.eq(id))
.fetchOne();
SafetyInspectionsRecord safetyInspection = load.fetchParent(Keys.LOADS__FK_SAFETY_INSPECTION);
So this way I am able to pull related records in a typesafe manner. The only annoying thing is I have to run another full query every time I call fetchParent or fetchDhild. Is there a way to eagerly fetch these all at once to avoid multiple round trips to the DB?
It is really nice to have these classes like LoadsRecord for CRUD screens, it makes updating the DB easy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用连接的经典方法
有多种方法可以实现一对一关系的具体化。最简单的一种是简单的
JOIN
或LEFT JOIN
(如果关系是可选的)。例如:
您可能希望随后使用生成的记录,因此您可以使用各种映射工具将通用
Record
类型映射到两个UpdatableRecord
类型以进行 进一步的 CRUD:嵌套记录
从 jOOQ 3.15 和 #11812,
MULTISET
和ROW
运算符可用于创建嵌套集合和记录。因此,在您的查询中,您可以编写:这已经有助于将嵌套数据结构映射为所需的格式。从jOOQ 3.17和#4727开始,你甚至可以直接使用表表达式来生成嵌套记录:
这一新功能肯定会弥补 jOOQ 的最大差距之一。您甚至可以使用 隐式连接:
Classic approach using joins
There are various ways you can achieve materialising a to-one relationship. The simplest one being a simple
JOIN
orLEFT JOIN
if the relationship is optional.E.g.:
You probably want to work with the generated records thereafter, so you can use various mapping tools to map the generic
Record
types to the twoUpdatableRecord
types for further CRUD:Using nested records
Starting from jOOQ 3.15 and #11812,
MULTISET
andROW
operators can be used to create nested collections and records. So, in your query, you could write:That would already help map the nested data structures into the desired format. Starting from jOOQ 3.17 and #4727, you can even use table expressions directly to generate nested records:
This new feature is definitely going to close one of jOOQ's biggest gaps. You could even simplify the above using implicit joins to this: