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

Tuple2<int, list> requires elt_0 alias for ont to be mapped #450

Closed arnaudroger closed 7 years ago

arnaudroger commented 7 years ago

tested with query

with t(id, bar) AS (VALUES(1, 'hel'), (1, 'lo'), (2, 'bye')) select * from t

and mapper

JdbcMapper<Tuple2<Integer, List<Bar>>> mapper = 
    JdbcMapperFactory
                .newInstance()
                        .addKeys("id")
                .newMapper(new TypeReference<Tuple2<Integer, List<Bar>>>() {
        });

and it worked

got 2 Tuple2 with id set to 1 and 2. using jool tuples and sfm-tuples

johnlcox commented 7 years ago

I have two tables as part of a join.

ResourcePrivilege Join Table:

resource_id privilege_id
1 10
2 10
3 11

Privilege Table:

id name
10 write
11 read

With a select like:

select 
  privilege.id, privilege.name, resource_privileges.resource_id
from
  privilege
join resource_privileges on resource_privileges.privilege_id = privilege.id
order by resource_privileges.resource_id

I then have mapper code like that only worked once I added "elt_0" and aliased resource_privileges.resource_id to "elt_0".

JdbcMapper<Tuple2<Integer, List<Privilege>>> privilegeMapper = JdbcMapperFactory.newInstance()
          .addKeys("id")
          .addKeys("elt_0")
          .newMapper(new TypeReference<Tuple2<Integer, List<Privilege>>>() { });
arnaudroger commented 7 years ago

Thanks will check that in a week

Sent from my iPhone

On 30 Jun 2017, at 21:53, John Leacox notifications@github.com wrote:

I have two tables as part of a join.

ResourcePrivilege Join Table:

resource_id privilege_id 1 10 2 10 3 11 Privilege Table:

id name 10 write 11 read With a select like:

select privilege.id, privilege.name, resource_privileges.resource_id from privilege join resource_privileges on resource_privileges.privilege_id = privilege.id order by resource_privileges.resource_id I then have mapper code like that only worked once I added "elt_0" and aliased resource_privileges.resource_id to "elt_0".

JdbcMapper<Tuple2<Integer, List>> privilegeMapper = JdbcMapperFactory.newInstance() .addKeys("id") .addKeys("elt_0") .newMapper(new TypeReference<Tuple2<Integer, List>>() { }); — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

arnaudroger commented 7 years ago

I suspect Privilege is { int id; } ?

johnlcox commented 7 years ago

Yeah Privilege is basically an id and name:

class Privilege {
  public int id;
  public String name;
}
arnaudroger commented 7 years ago

so when the field is no renamed elt0 then it ends up at element1[0].id and privilege_id is mapped to element1[1].id because it matches against the "id" prop name. Also, I discover a bug in my test I only got one privilege for 10 because there is a bug that disables the append for that list #451

I'll see if I can make the integer field take precedence over the partial match for the first column.

arnaudroger commented 7 years ago

could you try with version 3.13rc1 that should work now.