[英]How can I select a column by name in jooq, when result record has two columns of different tables with the same name?
I am using Jooq and as a result of a query with some joins I have a list of Record .我正在使用 Jooq 并且作为带有一些连接的查询的结果,我有一个Record列表。
This record includes two columns with name "id" and I would need to select one of them.这条记录包括两列名为“id”的列,我需要选择其中之一。
This method has String as parameter and it is not failing when passing "id" (probably returning the first occurrence).此方法有 String 作为参数,并且在传递“id”时没有失败(可能返回第一次出现)。
The other alternative I saw is to use the method that receives an int with the index.我看到的另一种选择是使用接收带有索引的 int 的方法。 This would work ok but I would prefer to avoid it.
这可以正常工作,但我宁愿避免它。
The best alternative I found on this case is to use the method that receives Field as parameter.我在这种情况下找到的最佳选择是使用接收Field作为参数的方法。 By using Field we can specify the table and column name so we are sure that we are selecting the column that we want.
通过使用 Field 我们可以指定表和列名,这样我们就可以确定我们正在选择我们想要的列。
So basically if we have table A and B both with "id" columns on the result of the query we can just define a field所以基本上,如果我们的表 A 和 B 在查询结果中都有“id”列,我们可以定义一个字段
final Field<UUID> bId = tableB.ID;
And then when processing the results we can access it by然后在处理结果时,我们可以通过
query.fetch().forEach(r -> { UUID theId = r.get(bId); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.