Usage improvement of ArrayTypeHandler?

24 views
Skip to first unread message

Dirk Olmes

unread,
May 20, 2019, 1:51:22 AM5/20/19
to mybatis-user
Hi,

I'm getting my feet wet with mybatis for a project at work where we look for a simple, lightweight way to abstract from plain JDBC. That's mybatis 3.5.1 BTW.

I'm using a Postgresql database with a table that holds multiple values for a column. This is modeled using the java.sql.Array datatype. I can read Arrays from the database just fine using the org.apache.ibatis.type.ArrayTypeHandler but inserting is a bit more difficult. I had to create a custom TypeHandler to map between the Object[] from my mapped objects and the java.sql.Array that the ArrayTypeHandler expects.

Since the ArrayTypeHandler unwraps the object from the java.sql.Array in the extractArray method I'm wondering if it shouldn't do so on the writing side, too. I.e. if an Object[] is passed in as parameter object in setNonNullParameter, shouldn't an Array instance be transparently created by the type mapper and parameter object be passed in?

I thought I'd check for feedback before I go and invest time in a test/extension ...

-dirk

Iwao AVE!

unread,
May 20, 2019, 9:28:59 AM5/20/19
to mybatis-user

Hello Dirk,

Thank you for bringing that up!
I have been thinking about the same thing!
Could you open a new issue on the tracker or send a pull request, please?

A few vague ideas about the new implementation:

  • Use Connection#createArrayOf().
  • Use jdbcType as the first argument if supplied and use the default mapping (i.e. Appendix B of the JDBC spec) as a fallback?
  • If the parameter is an instance of java.sql.Array, just call setArray() to keep backward compatibility?
  • Should/can it support an array of user defined type?

Regards,
Iwao


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/aacae16e-133a-4595-a11d-82ab4c35e866%40googlegroups.com.

Dirk Olmes

unread,
May 21, 2019, 7:05:29 AM5/21/19
to mybati...@googlegroups.com
On 5/20/19 3:28 PM, Iwao AVE! wrote:
> Hello Dirk,

Hi Iwao,

Thanks for your feedback.

> Thank you for bringing that up!
> I have been thinking about the same thing!
> Could you open a new issue on the tracker or send a pull request, please?

I'm motivated to work on a PR but it'll take a while to make progress in
parallel to the day job.

> A few vague ideas about the new implementation:
>
> * Use |Connection#createArrayOf()|.

That's how my current implementation does it :-)

> * Use |jdbcType| as the first argument if supplied and use the default
> mapping (i.e. Appendix B of the JDBC spec) as a fallback?

This will be a tricky one. Using the jdbcType for creating the array
worked in my simple tests so looks like a good approach. Regarding the
Appendix B mappings - is there something already available in the
mybatis sources somewhere?

I've had trouble with ARRAYs vs jdbcType when attempting to insert NULL
values. If I specify jdbcType=ARRAY on the insert statement's parameter
mapping I lose the type info reqiured for creating the proper Array
type. If I specify jdbcType=BIGINT (or whatever type the array elements
consist of), the mybatis code does the wrong thing. I can debug through
the source again to bring up the concrete places - writing this off the
top of my head right now.

Any ideas how to approach NULL values?

> * If the parameter is an instance of |java.sql.Array|, just call
> |setArray()| to keep backward compatibility?

Definitely.

> * Should/can it support an array of user defined type?

Not my primary goal right now but I might take a look into that later.

-dirk

Dirk Olmes

unread,
May 22, 2019, 3:56:34 AM5/22/19
to mybatis-user


On Tuesday, May 21, 2019 at 1:05:29 PM UTC+2, Dirk Olmes wrote:
On 5/20/19 3:28 PM, Iwao AVE! wrote:
> Hello Dirk,

Hi Iwao,

Thanks for your feedback.

> Thank you for bringing that up!
> I have been thinking about the same thing!
> Could you open a new issue on the tracker or send a pull request, please?

I'm motivated to work on a PR but it'll take a while to make progress in
parallel to the day job.


Meanwhile I managed to work on this a bit more. This is my first version of the improvement:  https://github.com/mybatis/mybatis-3/pull/1548

-dirk

Iwao AVE!

unread,
May 22, 2019, 9:05:54 AM5/22/19
to mybatis-user
Thanks a lot, Dirk !
I'll review as soon as possible.

Regards,
Iwao



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages