Mapper Select with Java record type and custom list TypeMappers

30 views
Skip to first unread message

Sydney Osborn

unread,
Nov 22, 2024, 5:40:06 PM11/22/24
to mybatis-user
1. I would like to use java record types as my model with mybatis Mappers
2. We are using the Mapper interface and a Configuration class with a ConfigurationCustomizer Bean (no xml)
3. I have multiple TypeMappers that address converting different Lists to Postgres Arrays (i.e., List<String>, List<MyType>)

Issues
1. I don't see how to register mulitiple List<x> type handlers in the TypeHandlerRegistry. It's not possible afaict to declare the Class<> type of a generic list, and therefore one can only effectively register one handler.  If' I'm wrong about this, then the rest of my issues might go away, but I've seen no examples.
2. Given that I can't explicitly register a type handler to handle my List<MyType> -> PgArray, I need to specify it in the  @Select statement somehow.  I can accomplish this in my @Insert statement, but the @Select statement only allows me to do this in an @Results annotation. However, when I do that, I get "There is no setter for property named X" errors as it appears this somehow  bypasses my record constructor.

Just as an example:
```
public record MyModel(UUID id,  List<MyType> myTypes, List<String> myStrings) {}

CREATE TABLE my_model (id uuid, my_types text[], my_strings text[])

@Mapper
public interface MyModelMapper {
    @Insert("""INSERT INTO my_model (my_types, my_strings) VALUES (
        #{model.myTypes,typeHandler=MyTypeListTypeHandler},
        #{model.myStrings, typeHandler=StringListTypeHandler}
    )
    @Options(flushCache = Options.FlushCachePolicy.TRUE, useGeneratedKeys = true, keyProperty = "model.id", keyColumn = "id")
    int createMyModel(@Param("model") MyModel model);

    @Select(
        """SELECT mymodel.id, mymodel.my_types, mymodel.my_strings
          FROM mymodel
          WHERE fixture_id = #{id}::uuid
        """)
        @Results({
             @Result(column="id",property="id"),
             @Result(column="my_types", property="myTypes, "typeHandler="StringListTypeHandler.class"),
             @Result(column="my_strings", property="myStrings", typeHandler=MyTypeListTypeHandler.class)
        })
        MyModel getById(@Param("id") UUID id);
}
```

Can I do what I want?

Thank you!

Iwao AVE!

unread,
Nov 22, 2024, 6:12:03 PM11/22/24
to mybati...@googlegroups.com
Hello Sydney,

You are right about issue 1 i.e. it is currently not possible to register type handlers against `List` with type parameters.

Regarding issue 2, as the mapping is done via constructor, you need to use `@ConstructorArgs` and `@Arg` instead of `@Results` and `@Result`.
I would also recommend reading the section explaining how the constructor-mapping works.

Let us know if you have any difficulties.

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 visit https://groups.google.com/d/msgid/mybatis-user/e4fba156-ee48-422d-84be-11ea926fad2an%40googlegroups.com.

Sydney Osborn

unread,
Nov 22, 2024, 6:35:42 PM11/22/24
to mybati...@googlegroups.com
Thanks, that is very helpful.  However, it seems like I still have the same problem providing a javaType for my List<> attributes which are being treated as Object, despite the type handler, right?

You received this message because you are subscribed to a topic in the Google Groups "mybatis-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mybatis-user/G5-5ZpBmygY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mybatis-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2RRyooPX5pv3VAmhkDW-ojHgcz%2BK6Zym08D5GU6B6AHNA%40mail.gmail.com.

Sydney Osborn

unread,
Nov 22, 2024, 6:39:16 PM11/22/24
to mybati...@googlegroups.com
Oh, `List.class` actually seems to work here.  
Reply all
Reply to author
Forward
0 new messages