V6.2.0

  • #604 map unsorted one to many relationship

By default for the one to many to map properly the rows need to be sorted by the root object

id name subs_id subs_name
1 name1 1 sub1
1 name1 2 sub2
2 name2 3 sub3
2 name2 4 sub4

will give you the right result

{
  "id": 1,
  "name": "name1",
  "subs": [
    { "id": 1, "name": "sub1" },
    { "id": 2, "name": "sub2" }
  ]
}    
{
  "id": 2,
  "name": "name2",
  "subs": [
    { "id": 3, "name": "sub3" },
    { "id": 4, "name": "sub4" }
  ]
}

but if the result are not in order like

id name subs_id subs_name
1 name1 1 sub1
2 name2 3 sub3
1 name1 2 sub2
2 name2 4 sub4

you will then get

{ "id" : 1, "name" : "name1", "subs" : [ { "id" : 1, "name" : "sub1" } ] }
{ "id" : 2, "name" : "name2", "subs" : [ { "id" : 3, "name" : "sub3" } ] }
{ "id" : 1, "name" : "name1", "subs" : [ { "id" : 2, "name" : "sub2" } ] }
{ "id" : 2, "name" : "name2", "subs" : [ { "id" : 4, "name" : "sub4" } ] }

Now if you are ready to pay the cost - increase processing and memory foot print - sfm can deal with that.

you will need to create the mapper using the unorderedJoin()

for example using the JdbcMapperFactory - it will be the same for all other except for POI where it’s not yet supported.

JdbcMapperFactory
    .newInstance()
    .unorderedJoin()
    .addKeys("id", "subs_id")
    .newMapper(MyObject.class);

and the result will be back to

{
  "id": 1,
  "name": "name1",
  "subs": [
    { "id": 1, "name": "sub1" },
    { "id": 2, "name": "sub2" }
  ]
}    
{
  "id": 2,
  "name": "name2",
  "subs": [
    { "id": 3, "name": "sub3" },
    { "id": 4, "name": "sub4" }
  ]
}

the order of the root object will be their first order of appearance in the rows.