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
438 stars 76 forks source link

Null elimination has no working annotation. #693

Open johnnyahlstedt opened 4 years ago

johnnyahlstedt commented 4 years ago

Hi, It would be nice if there was an annotation to be used when doing null elimination. Like @Key on id.

It works lika a charm with ex. JdbcMapper mapper = JdbcMapperFactory .newIntance() .addKeys("id", "students_id") .newMapper(Professor.class);

But when instead trying with @Key on students_id and a processor with no students, I get a List with size 1 with null value.

Keep up the good work! Johnny

arnaudroger commented 4 years ago

Should be quite easy to do

arnaudroger commented 4 years ago

actually just rereading that, could you give me some sample data that generate that? alright I see now

arnaudroger commented 4 years ago

so just tried to reproduce with the following code

       @Test
    public void testIssue693() throws SQLException {
        Connection dbConnection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);

        if (dbConnection == null) return;

        try {

            JdbcMapper<Prof693> mapper =  JdbcMapperFactory
                    .newInstance()
                    .newMapper(Prof693.class);;
            ;

            Statement st = dbConnection.createStatement();

            Iterator<Prof693> iterator = mapper.iterator(st.executeQuery("SELECT 1 as id , 'p1' as name, null as students_id, 's1' as students_name, 1::bit as students_test "));

            assertTrue(iterator.hasNext());

            assertEquals(new Prof693(1l, "p1", Collections.emptyList()), iterator.next());

        } finally {
            dbConnection.close();
        }

    }

    public static class Prof693 {
        @Key
        Long id;
        String name;
        List<Student693> students;

        public Prof693(Long id, String name, List<Student693> students) {
            this.id = id;
            this.name = name;
            this.students = students;
        }

        public Long getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public List<Student693> getStudents() {
            return students;
        }

        @Override
        public String toString() {
            return "Prof{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", students=" + students +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Prof693 prof = (Prof693) o;

            if (id != prof.id) return false;
            if (name != null ? !name.equals(prof.name) : prof.name != null) return false;
            return students != null ? students.equals(prof.students) : prof.students == null;
        }

        @Override
        public int hashCode() {
            int result = (int) (id ^ (id >>> 32));
            result = 31 * result + (name != null ? name.hashCode() : 0);
            result = 31 * result + (students != null ? students.hashCode() : 0);
            return result;
        }
    }

    public static class Student693 {
        @Key
        Long id;
        String name;
        boolean test;

        public Student693(Long id, String name, boolean test) {
            this.id = id;
            this.name = name;
            this.test = test;
        }

        public Long getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public boolean getTest() {
            return test;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", test='" + test + '\'' +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Student693 student = (Student693) o;

            if (id != student.id) return false;
            if (test != student.test) return false;
            return name != null ? name.equals(student.name) : student.name == null;
        }

        @Override
        public int hashCode() {
            int result = (int) (id ^ (id >>> 32));
            result = 31 * result + (name != null ? name.hashCode() : 0);
            result = 31 * result + (test ? 1 : 0);
            return result;
        }
    }

and it works, where is the @key annotation? what is the full name of the annotation package.simplename ? which version of sfm?