1

I'm using PostgreSQL. One of my table fields is an array:

"day_of_month" int[] DEFAULT NULL

But my domain object for this table is List<Integer>. I want to fetch the attribute value in of the attribute in POJO with type :List<Integer> using MyBAtis query API.

Is there any automatic way to do this, is there anything provided by Postgresql to directly convert it into List<Integer>, wanted to avoid extra processing?

1 Answer 1

1

You have to use custom TypeHandler, an example here.

You are specifically interested in getResult method, statement.getArray(i).getArray() will return an Object[]. Just put it into a List (or anything you want): Arrays.asList() and return it. By the way, don't forget null check to avoid NPE.

Of course, reference it in your resultMap with attribute typeHandler="type.handler.full.qualified.name"

Sign up to request clarification or add additional context in comments.

2 Comments

yeah...this is one of the alternatives...but I wanted something in postgresql query itself.
yeah, that's what you asked. But I wonder what is the purpose? You seem to use a "cast"-like syntax, but it could only work for input. What you want would involve Postgres to "know" what is a java List, but why should it? The jdbc driver provides a resultSet to fetch results as "simple" java types. Mybatis is another layer over that to map to more complex structures such as List.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.