Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using a DTO which contains Set<UUID> and Set<String>.

    public class MyBatisDTO implements Serializable{
    
    // other attributes
    
        private Set<UUID> uuidSet;
    
        private Set<String> stringSet;
    
      .....
}

A typeHandler has already been registered for Generic-Type Set<T>.

@MappedJdbcTypes(JdbcType.OTHER)
@MappedTypes(Set.class)
public class SetTypeHandler extends BaseTypeHandler<Set<T>> {


    @Override
    public Set<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return mapFromJson(rs.getString(columnName));
    }
 .......

Now the problem is that when above mentioned MyBatisDTO.java is being mapped from DB values where both columns uuidSet and stringset are stored as jsonb values, typeHandler picks the Generic type as String -> T extends String for Set<UUID> uuidSet; as well which is leading to auto-type conversion to all items of uuidSet as String. This is not an error but in debugging it can be seen that uuidSet contains String values. Later on, which also leads in de-serialization of JAX-RS response but thats another topic which is not important at the moment.
My question is Is there a way that we can enforce a typeHandler on property's level? so I am looking for a solution in which I may attach custom TypeHandler on property level, something like this

//SUDO_CODE

    public class MyBatisDTO implements Serializable{
            
            // other attributes
            
                @TypeHandler ("com.mybatis.handlers.typeHandler.SetUUIDTypeHandler")
                private Set<UUID> uuidSet;
            
                @TypeHandler ("com.mybatis.handlers.typeHandler.SetStringTypeHandler")
                private Set<String> stringSet;
    .....
    }

OR if there is a way to mention these typeHandlers on Mapper-Level, something like this?

@Mapper
public interface MyBatisMapper {

 
//TYPE HANDLER ATTACHMENT/CONFIGURATION HERE ..???
    @Select("SELECT uuidSet, stringSet FROM MyBatisDTO WHERE param = #{param}")
    Cursor<MyBatisDTO> getData(@Param("param") final UUID param);
.....
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
621 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can do it with a result map:

@Mapper
public interface MyBatisMapper {
    @Select("SELECT uuidSet, stringSet FROM MyBatisDTO WHERE param = #{param}")
    @Results({
      @Result(column = "uuidSet", property="uuidSet", typeHandler = "com.mybatis.handlers.typeHandler.SetUUIDTypeHandler"),
      @Result(column = "stringSet", property="stringSet", typeHandler = "com.mybatis.handlers.typeHandler.SetStringTypeHandler")
    })
    Cursor<MyBatisDTO> getData(@Param("param") final UUID param);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...