In jOOQ, it is easy to use bind variables in expressions like:
MY_TABLE.COL.eq(1);
MY_TABLE.COL.in(1, 2, 3);
MY_TABLE.COL.between(1).and(10);
All of the above methods accept both T
and Field<T>
types, e.g. Field.eq(T)
or Field.eq(Field<T>)
. But what if I want to put the bind variable on the left side of the expression? These obviously don't work:
1.eq(MY_TABLE.COL);
1.in(MY_TABLE.COL1, MY_TABLE.COL2, MY_TABLE.COL3);
1.between(MY_TABLE.COL1).and(MY_TABLE.COL2);
How can I turn those 1
or "xyz"
into Field<Integer>
or Field<String>
, etc.?
(This is such a popular question on the jOOQ mailing list, etc., so I'm documenting it here)
The methods accepting T
are just convenience methods. Behind the scenes, they all wrap the T
argument in DSL.val(T)
// This
MY_TABLE.COL.eq(1);
// Is just convenience for this:
MY_TABLE.COL.eq(val(1));
// Or rather, this one to be precise
MY_TABLE.COL.eq(val(1, MY_TABLE.COL.getDataType()));
Because wrapping everything in val()
would be very verbose and cumbersome, all of jOOQ's API accepting Field<T>
also accepts T
for convenience. If Java had untagged union types, the parameter types of such methods would be T|Field<T>
.
In rare cases, users need to explicitly wrap their bind values in val()
, including:
select()
Field<T>
type for one of the parametersSo, just write:
val(1).eq(MY_TABLE.COL);
val(1).in(MY_TABLE.COL1, MY_TABLE.COL2, MY_TABLE.COL3);
val(1).between(MY_TABLE.COL1).and(MY_TABLE.COL2);
Note there's also DSL.inline(T)
to create "inline values", or also "constants", "literals", instead of bind values.
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With