Hi Samir,
I'm guessing that the problem might be in your overridden sql() method:
@Override
public void sql(BindingSQLContext<JsonNode> ctx) throws SQLException {
// Rendering a bind variable for the binding context's value and
casting it to the json type
//Allows for null values to be stored in DB
if (ctx.convert(converter()).value() == null) {
ctx.render().visit(DSL.val(ctx.convert(converter()).value(),
JsonNode.class));
} else {
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::json");
}
In particular, there's not really a point in the if branch of your if-else
statement - why would you re-delegate the generation of your SQL string to
another instance of some JsonNode binding? Do note, if you don't explicitly
specify the binding on a DSL.val() bind variable, jOOQ will internally pick
one from a static data type registry, i.e. the first one jOOQ has ever seen
on the class path for the JsonNode.class
This might be an entirely different implementation from the one you're
using here.
Another thing in PostgresJsonBinding.converter():
@Override
public Object to(JsonNode u) {
return u == null || u ==
JsonNodeFactory.instance.objectNode() ? null : u.toString();
}
Are you sure about that object identity comparison? I mean, looking at the
implementation, the objectNode() method returns a new instance every time,
so identity comparison doesn't really make sense.
Of course, there could be entirely different reasons why your binding might
not even be applied to the insert statement. I can only be really sure if I
have an MCVE:
https://stackoverflow.com/help/mcve
Thanks,
Lukas
On Thu, Aug 30, 2018 at 6:43 PM Samir Faci <[email protected]> wrote:
> Attached are the two files I've used to allow me to read JSON postgres
> fields in jooq.
>
> The use case i'm running into is where I want to explicitly set the field
> to null but the Jooq (or my implementation ) stores it as an empty JSON
> object.
>
> My implementation is roughly based on:
> https://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings/
> but using Jackson JSON library
> instead of GSON.
>
> I've tried to adjust the behavior as you can see from the snippet in
> PostgresJsonBinding but that isn't working as expected. The code gets
> invoked
> but i'm still seeing '{}' being stored rather then nulls.
>
> What am I missing here?
>
>
> --
> Thank you
> Samir Faci
> https://keybase.io/csgeek
>
> --
> 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/d/optout.
>
--
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/d/optout.