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

ValueObject with null values #755

Closed mcherb closed 2 years ago

mcherb commented 3 years ago

I have this query :

create view actor_reading
select a.id, a.name, m.title as movies_title, m.gender as movies_gender, n.label as nationality
from actor a
  left join actor_movie_mapping amm on a.id = amm.actor_id
  left join movie m on m.id = amm.movie_id
  left join nationality n on a.nationality_id = n.id
order by a.id

as you can see I'm joining the actor with 2 tables to get all its movies and at the same time its nationality.

Mapping classes are like the following :

@Value //lombok
class Actor {
 String id;
 String name;
 String nationality;
 List<Movie> movies;
}

@Value //lombok
class Movie {
 String title;
 Integer gender;
}
ResultSetExtractor<List<Actor>> resultSetExtractor =
        JdbcTemplateMapperFactory
                .newInstance()
                .addKeys("id")
                .newResultSetExtractor(Actor.class);
jdbcTemplate.query("select * from actor_reading", resultSetExtractor);

I get my actor's list but in some objects where the actor didn't have played in any movie yet the list movies may contains objects like :

movies = [Movie(title= null, gender = null)] // or multiple values if the actor have multiple nationalities

Any idea to get rid of these objects ?

n1b0r commented 3 years ago

I think you should insert "movies_title" in the addKeys() method.

see Null elimination chapter : https://simpleflatmapper.org/0203-joins.html

mcherb commented 3 years ago

yes, but in my real case I can't get a uniq id, I want objects to be duplicated (sometimes)

ltkn commented 3 years ago

I think it causes less issues if each joined table have a child object. you could have a list of Nationality and like @n1b0r mentioned add all primary keys in addKeys(movies_id, nationality_id)