Provide a way to get EntityRowMapper
I want to reuse the EntityRowMapper to query using JdbcTemplate, currently I can't find an easy way to get the mapper for an entity class and have to copy paste code from DefaultDataAccessStrategy to create one. It would be nice if you could support this some how.
Have you consulted the documentation?
- Custom RowMapper or ResultSetExtractor
-
@Queryannotation withrowMapperRef/rowMapperClassattributes
Other than that, we do not accept RowMapper query method arguments.
I think you miss understand me, I'm not using @Query annotation but using JdbcTemplate/NamedParameterJdbcTemplate to execute some query built at runtime. This query will return the entity so it's would be nice if I can reuse the EntityRowMapper class of spring data jdbc to map the result to entity.
Basically like this:
var entityRowMapper = //Some how get spring data jdbc row mapper for entity class
var user = namedParameterJdbcTemplate.queryForObject(
"select * from users where id = :id",
Map.of("id", "0001"),
entityRowMapper
);
For me, the advantage to use an EntityRowMapper is that it handles the Embedded.OnEmpty as I have Embedded object. That way the mapping is homogeneous with the usage of JdbcAggregateTemplate.
I've found this simple workaround for now (which maybe the same as @VietAnh14), I inject JdbcConverter in my custom repository and then I create my own EntityRowMapper :
new EntityRowMapper<>(
(RelationalPersistentEntity<MyEntity>) this.jdbcConverter.getMappingContext().getRequiredPersistentEntity(MyEntity.class),
this.jdbcConverter
)