Let's say I want to select the max value of a specific field:
getContext().select( ..., //several fields
DSL.max(MY_TABLE.SCORE).as("max_score"))
.from(MY_TABLE)
.where(...)
.groupBy(...);
I fetch the results, and now I want to get the values of a specific record, so I do:
Integer maxScore = (Integer) record.get("max_score");
This is working just fine, but I have 2 issues with this approach:
Integer
.int
instead of Integer
, I get a warning on this line:
Unboxing of 'record.get("max_score")' may produce 'NullPointerException'
.
For table's "not-null" fields, I don't get this warning.Is there a way to overcome these issues?
Type safety is lost - I must cast the value to Integer.
You can just assign it to a local variable:
Field<Integer> maxScore = max(MY_TABLE.SCORE).as("max_score");
Use it like this:
.select(..., maxScore)
.from(...)
And then:
record.get(maxScore);
If I define maxScore as int instead of Integer, I get a warning [...]
The warning is likely to go away with the above approach. While Record.get(String)
returns @Nullable Object
, Record.get(Field<T>)
just returns T
(inheriting T
's nullability, which is likely to be unspecified).