Bind a Postgresql array to a java list instead of a java Array.

39 views
Skip to first unread message

Max Kremer

unread,
Aug 21, 2019, 1:12:56 PM8/21/19
to jOOQ User Group
Using Jooq 3.10 and Postgresql 10 - Not using the code generator 

I'm trying to improve some code that uses primitive arrays, specifically String[ ] . As everyone knows arrays can be clunky to work with, Lists are much preferred but I can't figure out how to bind a PostgreSQL array field to a List<T>


..
..
public final TableField<EventRecord, String[]> TAGS = createField("tags", SQLDataType.VARCHAR.getArrayDataType(), this);
..
..


and in my pojo I have 

private String[] tags;
..
...
....

@Column(name="tags")
 
 
public void setTags(String[] tags) {
     
this.tags = tags;
 
}


I want to change the type of tags in the pojo from String[]  to ArrayList<String>

How would I get JOOQ to map the postgresql array into a collection? 

Is there a createField equivalent that has built in conversion from arrays to collections or must I write a custom converter?

Seems like a typical use case but alas searches of docs, the group, and stackoverflow have come up empty.

Lukas Eder

unread,
Aug 22, 2019, 3:49:52 AM8/22/19
to jOOQ User Group
Hi Max,

The main reason why jOOQ prefers arrays over lists in such cases is the fact that the array component type is not erased at runtime. Quite a bit of jOOQ's internals depends on being able to reflect on the type of a column, and in case of arrays, the component type of the array.

Obviously, if you write your own, specific data type binding per array type, you do not have this limitation.

Whether you're using the code generator or not is not important here. You will need to use the same mechanisms manually, instead of automating the task using code generation. Since you're using the internal APIs (like createField()), I do recommend again to use the code generator as little is gained from not doing so. In this case, you'd attach your data type binding using a <forcedType/> configuration.

There is a createField() overload where you can pass your Binding<String[], ArrayList<String>> implementation. You will simply need to use that overload instead of the one you were using, and you're set.

I will be very happy to help you with additional specific questions you may have.

Best Regards,
Lukas

--
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 jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/b6ec9f48-4b15-4adb-8237-0415a2933282%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages