spring-projects / spring-projects/spring-data-relational
Custom class is registered as simple type only if there is writing converter
@schauder is already working on this.
Since Mar 10, 2022.
- Dominant language
- Java
- Stars
- 827
- Forks
- 394
- PR merge metrics
- No merged PRs in 30d
Description
I am using Spring Data JDBC repository to query data for report (hence, readonly). In one place I needed to convert from JSONB to a DTO. I registered a customer converter marked as @ReadingConverter for that purpose, but for some reason it was never called. Debugging the code, I noticed that my DTO is considered as an entity, although it should have been registered as a simple type. Debugging more I found out that only the source type of a @WritingConverter is registered as a simple type. Adding a dummy writing converter fixed the issue for me. However, this seems unexpected and the fix adds redundant code.
My code:
@Data
public class ReportDTO {
private MyData myData;
}
@Configuration
public class JdbcConfig extends AbstractJdbcConfiguration
private final ObjectMapper objectMapper = new ObjectMapper();
@Bean
@NonNull
@Override
public JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions(
List.of(
new JsonbToMyData(objectMapper),
)
);
}
@ReadingConverter
@RequiredArgsConstructor
static class JsonbToMyData implements Converter<PGobject, MyData> {
private final ObjectMapper objectMapper;
@Override
public MyData convert(PGobject source) {
try {
return objectMapper.readValue(source.getValue(), MyData.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.