Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<association> with Nested <collection> Returns Unexpected Results #1835

Closed
UserProgrammer opened this issue Feb 13, 2020 · 4 comments
Closed

Comments

@UserProgrammer
Copy link

UserProgrammer commented Feb 13, 2020

MyBatis version

3.4.6

Database vendor and version

mysql-connector-java:8.0.19

Test case or example project

https://github.com/UserProgrammer/mybatis-discriminator-example/tree/resultmap-bug-hunt

The relevant result-map xml configuration can be found in
src/main/resources/com/example/dao/CarDao.xml

The unit test is located at src/test/java/com/example/dao/ICarDaoTest.java

database sql scripts are located at sql-scripts/

Steps to reproduce

  1. Clone repo (resultmap-bug-hunt branch)
  2. In the terminal, type script/build in the root project directory. (It'll take a few moments for the database docker container to be ready, you'll see a repeated message from mysqladmin. This is expected)

Expected result

[
   {
      "car":{
         "id":1,
         "vin":"13icf043",
         "year":"2019",
         "make":"Subaru",
         "model":"Legacy",
         "color":"black",
         "vehicleType":"car",
         "serviceRecordIds":[
            1, 2, 3
         ],
         "doorCount":4
      },
      "user":{
         "id":1,
         "firstName":"Michael",
         "lastName":"da Costa"
      }
   }, {
      "car":{
         "id":2,
         "vin":"4tgu4985",
         "year":"2014",
         "make":"Ford",
         "model":"Escape",
         "color":"red",
         "vehicleType":"car",
         "serviceRecordIds":[

         ],
         "doorCount":5
      },
      "user":{
         "id":2,
         "firstName":"John",
         "lastName":"Doe"
      }
   }
]

Actual result

Note that there are separate results for each serviceRecordId (for car with id = 1), instead of having a single result where all the serviceRecordIds are collected in an array, as is expected:

[
   {
      "car":{
         "id":1,
         "vin":"13icf043",
         "year":"2019",
         "make":"Subaru",
         "model":"Legacy",
         "color":"black",
         "vehicleType":"car",
         "serviceRecordIds":[
            1
         ],
         "doorCount":4
      },
      "user":{
         "id":1,
         "firstName":"Michael",
         "lastName":"da Costa"
      }
   },
   {
      "car":{
         "id":1,
         "vin":"13icf043",
         "year":"2019",
         "make":"Subaru",
         "model":"Legacy",
         "color":"black",
         "vehicleType":"car",
         "serviceRecordIds":[
            2
         ],
         "doorCount":4
      },
      "user":{
         "id":1,
         "firstName":"Michael",
         "lastName":"da Costa"
      }
   },
   {
      "car":{
         "id":1,
         "vin":"13icf043",
         "year":"2019",
         "make":"Subaru",
         "model":"Legacy",
         "color":"black",
         "vehicleType":"car",
         "serviceRecordIds":[
            3
         ],
         "doorCount":4
      },
      "user":{
         "id":1,
         "firstName":"Michael",
         "lastName":"da Costa"
      }
   },
   {
      "car":{
         "id":2,
         "vin":"4tgu4985",
         "year":"2014",
         "make":"Ford",
         "model":"Escape",
         "color":"red",
         "vehicleType":"car",
         "serviceRecordIds":[

         ],
         "doorCount":5
      },
      "user":{
         "id":2,
         "firstName":"John",
         "lastName":"Doe"
      }
   }
]
@harawata
Copy link
Member

Hello @UserProgrammer ,
The linked repo is not found. Could you check please?

@UserProgrammer
Copy link
Author

@harawata My bad. I had set it up as a private repo. It should be available now.

@harawata
Copy link
Member

Thank you for the update, @UserProgrammer ,
I could clone the repo!

It seems like a simple misconfiguration issue.
The query returns 4 rows:

vehicle_id vehicle_vin vehicle_year vehicle_make vehicle_model vehicle_color vehicle_type car_door_count id first_name last_name service_record_id
1 13icf043 2019 Subaru Legacy black car 4 1 Michael da Costa 1
1 13icf043 2019 Subaru Legacy black car 4 1 Michael da Costa 2
1 13icf043 2019 Subaru Legacy black car 4 1 Michael da Costa 3
2 4tgu4985 2014 Ford Escape red car 5 2 John Doe null

MyBatis needs to know which column(s) to use to group these rows, so you need to declare <id /> in the result map.

<resultMap id="carOwnershipResultMap" type="com.example.entity.CarOwnership">
  <id column="vehicle_id" /><!-- added -->
  <association property="car" resultMap="carResultMap" />
  <association property="user" resultMap="userResultMap" />
</resultMap>

Note that property attribute is omitted because there is no corresponding property in CarOwnership.

With the above result map, the test passed on my environment.
Please try it and let us know the result.

@UserProgrammer
Copy link
Author

UserProgrammer commented Feb 14, 2020

That worked! I thought the <resultMap="carOwnershipResultMap"> picked up on the <id .../> tag in <resultMap id="vehicleResultMap">. Thank you for taking the time to look into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants