arnaudroger / SimpleFlatMapper

Fast and Easy mapping from database and csv to POJO. A java micro ORM, lightweight alternative to iBatis and Hibernate. Fast Csv Parser and Csv Mapper
http://simpleflatmapper.org
MIT License
439 stars 76 forks source link

sfm-springjdbc: IllegalStateException: Invalid state currentKey is null when mapping one to many #624

Closed neVERberleRfellerER closed 5 years ago

neVERberleRfellerER commented 5 years ago

With Spring Boot (spring-boot-starter-jdbc) 2.1.3.RELEASE on

openjdk 11.0.3 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+4)
OpenJDK 64-Bit Server VM (build 11.0.3+4, mixed mode)

with this schema

https://github.com/arnaudroger/SimpleFlatMapper/blob/sfm-parent-6.6.0/sfm-examples/sfm-jooq/src/main/resources/db-h2.sql

this simple code

@Configuration
public class ErrorRunnerConfig {

    @Bean
    public CommandLineRunner errorRunner(JdbcTemplate template) {
        RowMapper<Author> rowMapper = JdbcTemplateMapperFactory.newInstance()
                .addKeys("id", "books_id")
                .newRowMapper(Author.class);

        return (args) -> {
            Iterable<Author> authors = template.query("select a.id as id, a.first_name as first_name, a.last_name as last_name, b.id as books_id, b.title as books_title from author a left join book b on b.author_id = a.id order by a.id asc", rowMapper);
            for (Author author : authors) {
                System.out.println(author);
            }
        };
    }

    public static class Author {
        public Long id;
        public String firstName;
        public String lastName;
        public List<Book> books;
    }

    public static class Book {
        public Long id;
        public String title;
    }

}

causes IllegalStateException:

Caused by: java.lang.IllegalStateException: Invalid state currentKey is null
        at org.simpleflatmapper.map.context.impl.BreakDetector.getValue(BreakDetector.java:81) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.map.context.impl.BreakDetector.getValue(BreakDetector.java:74) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.map.context.impl.BreakDetectorMappingContext.getCurrentValue(BreakDetectorMappingContext.java:78) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.map.fieldmapper.MapperFieldMapper.mapTo(MapperFieldMapper.java:36) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.map.generated.com.example.demo.AsmMapperFromResultSetToAuthorInj4_I5.mapFields(Unknown Source) ~[na:na]
        at org.simpleflatmapper.map.generated.com.example.demo.AsmMapperFromResultSetToAuthorInj4_I5.mapFields(Unknown Source) ~[na:na]
        at org.simpleflatmapper.map.mapper.AbstractMapper.map(AbstractMapper.java:23) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.map.mapper.ContextualSourceFieldMapperImpl.map(ContextualSourceFieldMapperImpl.java:31) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.map.mapper.AbstractEnumerableDelegateMapper.map(AbstractEnumerableDelegateMapper.java:18) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.jdbc.JdbcMapperBuilder$JdbcMapperImpl.map(JdbcMapperBuilder.java:241) ~[sfm-jdbc-6.6.0.jar:na]
        at org.simpleflatmapper.jdbc.JdbcMapperBuilder$JdbcMapperImpl.map(JdbcMapperBuilder.java:230) ~[sfm-jdbc-6.6.0.jar:na]
        at org.simpleflatmapper.map.mapper.DynamicSetRowMapper.map(DynamicSetRowMapper.java:45) ~[sfm-map-6.6.0.jar:na]
        at org.simpleflatmapper.jdbc.JdbcMapperFactory$DynamicJdbcSetRowMapper.map(JdbcMapperFactory.java:292) ~[sfm-jdbc-6.6.0.jar:na]
        at org.simpleflatmapper.jdbc.spring.RowMapperImpl.mapRow(RowMapperImpl.java:55) ~[sfm-springjdbc-6.6.0.jar:na]
        at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:61) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:440) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:376) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:452) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:462) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]

It does not throw any error when addKeys is commented out or when addKeys reference only non-existent columns.

neVERberleRfellerER commented 5 years ago

My fault. I swapped RowMapper and ResultSetExtractor and even when I was chekcing the docs I though that I use ResultSetExtractor. Accept my humble apology.

arnaudroger commented 5 years ago

well it should not fail on the row mapper there anyway, I think the Row mapper should prob not deals with the join and still work or throw an exception when requesting the row mapper.