Search code examples
javasql-updatejooq

How to create 'update' using multiple 'set' methods


Synopsis: I'm trying to create an SQL update using jOOQ

DSL.using(connection)
.update(DSL.table("dogs"))
.set(DSL.field("age"), DSL.field("age").add(1))
.set(DSL.field("rabies"), "true")
.where(DSL.field("id").eq("Kujo"))
.execute();

Issue:

The method set(Field<Object>, Object) is ambiguous for the type UpdateSetFirstStep<Record>

Question: How do I create this update using jOOQ?


Solution

  • You ran into this problem: Reference is ambiguous with generics

    Fixing your query

    It's always a good idea to attach data types with your jOOQ expressions. In your particular case, you can work around the problem by specifying things like:

    DSL.field("age", SQLDataType.INTEGER)
    

    Or, shorter, with the usual static imports:

    field("age", INTEGER)
    

    Using the code generator

    However, jOOQ is best used with its code generator, see also this article here. Not only will you avoid problems like these, but you also get compile time type safety (of data types and meta data), advanced features like implicit joins and much more.

    Your query would then look like this:

    DSL.using(connection)
       .update(DOGS)
       .set(DOGS.AGE, DOGS.AGE.add(1))
       .set(DOGS.RABIES, true)
       .where(DOGS.ID.eq("Kujo"))
       .execute();