J'utilise mybatis-spring-boot-starter avec la version 2.1.0. J'ai besoin de traiter le type UUID pour obtenir une collection imbriquée.
@Select("SELECT id, name FROM t_service s")
@Results(value = {
@Result(column = "id", property = "id", jdbcType = JdbcType.OTHER, typeHandler = UuidTypeHandler.class),
@Result(column = "name", property = "name"),
@Result(property = "rates", column = "id", javaType = List.class, many = @Many(select = "getAllRates"))
})
List<Service> findAll();
@Select("SELECT date_from, date_to, currency FROM t_rate where t_rate.service_id = #{serviceId, javaType=java.util.UUID, jdbcType=OTHER, typeHandler=my.package.UuidTypeHandler}")
@Results(value = {
@Result(property = "dateFrom", column = "date_from"),
@Result(property = "dateTo", column = "date_to"),
@Result(property = "currency", column = "currency")
})
List<Rate> getAllRates(@Param("serviceId") UUID serviceId);
UuidTypeHandler :
@MappedJdbcTypes(JdbcType.OTHER)
@MappedTypes(UUID.class)
public class UuidTypeHandler extends BaseTypeHandler<UUID> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) throws SQLException {
ps.setObject(i, parameter, jdbcType.TYPE_CODE);
}
@Override
public UUID getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getObject(columnName, UUID.class);
}
@Override
public UUID getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getObject(columnIndex, UUID.class);
}
@Override
public UUID getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getObject(columnIndex, UUID.class);
}
}
Mais je reçois l'exception suivante :
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'serviceId' in 'class java.util.UUID'
Dans d'autres situations (lorsque les méthodes sont appelées directement), UuidTypeHandler fonctionne correctement et je n'ai aucun problème.
Solution de contournement pour PostgreSQL :
@Select("SELECT date_from, date_to, currency FROM t_rate where t_rate.service_id = #{serviceId}::uuid")
@Results(value = {
@Result(property = "dateFrom", column = "date_from"),
@Result(property = "dateTo", column = "date_to"),
@Result(property = "currency", column = "currency")
})
List<Rate> getAllRates(@Param("serviceId") String serviceId);