Stefan Falk
Stefan Falk

Reputation: 25457

How can I store XML data?

I saw How can I insert an XML document in PostgreSQL in Java? but I was wondering if jOOQ offers another solution to this kind of problem.

At the moment I don't see another method as to write something like

String sql = "INSERT INTO xml_docs(id, gkuzu) VALUES (?, XMLPARSE(?))";

and execute it with jOOQ

this.ctx.execute(sql);

but is there another option for more complex statements where I can also write e.g.

StringBuilder xml = new StringBuilder();

// ..

this.ctx.insertInto(MY_TABLE)
    .set(MY_TABLE.NAME, name)
    // ..
    .set(MY_TABLE.DESCRIPTION_XML, xml.toString());

or similar?

Upvotes: 1

Views: 1184

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221125

jOOQ 3.7 doesn't support XML (or JSON, etc.) types out of the box, but indeed, jOOQ helps you abstract the XML data type by implementing your custom data type binding. There's an example for JSON and PostgreSQL in the manual. You can adapt that easily for XML:

http://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings

Essentially, you'll need to implement org.jooq.Binding<Object, YourXMLRepresentation>, and then configure the code generator to apply this binding either to all XML type columns, or to columns of a specific name pattern:

http://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings

Upvotes: 1

Related Questions