Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select from only one table in jOOQ using a query with a join?

Tags:

java

mysql

jooq

I have the following query in 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.

like image 665
islon Avatar asked Dec 28 '12 19:12

islon


1 Answers

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):

PersonRecord record = factory()
  .select()
  .from(PERSON)
  .join(ENDUSER).on(ENDUSER.PERSON_FK.equal(PERSON.ID))
  .where(ENDUSER.ID.equal(userId))
  .fetchOne()
  .into(PERSON);
like image 129
Eric Avatar answered Sep 28 '22 11:09

Eric