spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

Provide a way to get EntityRowMapper

Open VietAnh14 opened this issue 1 year ago • 3 comments

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.

VietAnh14 avatar Nov 25 '24 03:11 VietAnh14

Have you consulted the documentation?

Other than that, we do not accept RowMapper query method arguments.

mp911de avatar Nov 25 '24 08:11 mp911de

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
      );

VietAnh14 avatar Nov 25 '24 08:11 VietAnh14

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
)

lpraud avatar Jan 04 '25 10:01 lpraud