如何使用带有连接的查询从jOOQ中的一个表中选择?

时间:2022-07-26 03:35:25

I have the following query in jOOQ:

我在jOOQ有以下查询:

factory()
.select()
.from(PERSON)
.join(ENDUSER).on(ENDUSER.PERSON_FK.equal(PERSON.ID))
.where(ENDUSER.ID.equal(userId))
.fetchOne();

This query returns to me a Record with all columns from PERSON and ENDUSER, but I only want the columns from PERSON (that's why I put .from(PERSON) and not .from(PERSON, ENDUSER)). I know it doesn't matter that much but I don't want unnecessary fields to be returned.

这个查询返回一个记录,其中包含PERSON和ENDUSER的所有列,但我只想要PERSON的列(这就是为什么我输入.from(PERSON)而不是.from(PERSON, ENDUSER)))。我知道这不重要,但我不希望返回不必要的字段。

2 个解决方案

#1


14  

You can access the fields in PERSON through the Table.fields() method:

您可以通过Table.fields()方法亲自访问字段:

factory()
.select(PERSON.fields()) // Or getFields() in jOOQ 2.x
.from(PERSON)
.join(ENDUSER)...

This is about the same as writing

这和写作是一样的。

SELECT PERSON.*
FROM PERSON
JOIN ENDUSER ...

Another option is to actually list all the fields from person one by one

另一种选择是逐一列出所有字段

#2


7  

Lukas's answer was exactly what I was looking for. You can also use the into() method to get a strongly typed response object back for only the table you care about (instead of the generic Record type):

卢卡斯的回答正是我想要的。您还可以使用into()方法只对您关心的表(而不是一般的记录类型)返回强类型响应对象:

PersonRecord record = factory()
  .select()
  .from(PERSON)
  .join(ENDUSER).on(ENDUSER.PERSON_FK.equal(PERSON.ID))
  .where(ENDUSER.ID.equal(userId))
  .fetchOne()
  .into(PERSON);

#1


14  

You can access the fields in PERSON through the Table.fields() method:

您可以通过Table.fields()方法亲自访问字段:

factory()
.select(PERSON.fields()) // Or getFields() in jOOQ 2.x
.from(PERSON)
.join(ENDUSER)...

This is about the same as writing

这和写作是一样的。

SELECT PERSON.*
FROM PERSON
JOIN ENDUSER ...

Another option is to actually list all the fields from person one by one

另一种选择是逐一列出所有字段

#2


7  

Lukas's answer was exactly what I was looking for. You can also use the into() method to get a strongly typed response object back for only the table you care about (instead of the generic Record type):

卢卡斯的回答正是我想要的。您还可以使用into()方法只对您关心的表(而不是一般的记录类型)返回强类型响应对象:

PersonRecord record = factory()
  .select()
  .from(PERSON)
  .join(ENDUSER).on(ENDUSER.PERSON_FK.equal(PERSON.ID))
  .where(ENDUSER.ID.equal(userId))
  .fetchOne()
  .into(PERSON);