Hi Darren,

There's currently no way to do this in a one-liner through jOOQ's API. This
is probably missing and would be useful for others, too:
https://github.com/jOOQ/jOOQ/issues/2833

The code you have posted more or less correspodns to what would be
implemented for the above #2833. Of course, the API implemented with #2833
would also work with composite keys.

Note that you might omit one step, though. Instead of

        return create().select()
                .from(table)
                .where(keyField.eq(converted))
                .fetchOneInto(clz);

Write this:

        return create().selectFrom(table)
                .where(keyField.eq(converted))
                .fetchOne();

Cheers
Lukas

2013/11/8 Darren S <[email protected]>

> I didn't find a simple API to do this, but this is what I figured out how
> to do that seems to accomplish what I wanted.  I'm not sure if this would
> be considered abusive, but it works quite well.
>
>         Class<UpdatableRecord<?>> clz = ...;
>
>         Table<?> table = getTable(clz);
>         if ( table == null )
>             return null;
>
>         UniqueKey<?> key = table.getPrimaryKey();
>         if ( key == null || key.getFieldsArray().length != 1 )
>             return null;
>
>         @SuppressWarnings("unchecked")
>         TableField<?, Object> keyField = (TableField<?,
> Object>)key.getFieldsArray()[0];
>
>         /* Convert object because we are abusing type safety here */
>         Object converted = keyField.getDataType().convert(id);
>
>         return create().select()
>                 .from(table)
>                 .where(keyField.eq(converted))
>                 .fetchOneInto(clz);
>
> Darren
>
>
> On Friday, November 8, 2013 1:18:55 AM UTC-7, Darren S wrote:
>>
>> I'm guessing this is probably a simple question.  If I have the primary
>> key value and an UpdatableRecord is there an API to find the record by
>> primary key?  Something that is just like the generated fetchById(..)
>> method on the DAO.  I see in the documentation that you can do
>> "create.fetchOne(BOOK, BOOK.ID.equal(id))," but I'm doing something with
>> reflection and it would be easier to not have to know the TableField.  So
>> I'm looking for something like DSLContext.findById(Class<UpdatableRecord>,
>> Object key).
>>
>> Darren
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "jOOQ User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to