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

Added selectMapWithList to handle non-unique column as a key #3239

Conversation

NiazBinSiraj
Copy link

Currently, when using selectMap() in MyBatis, the key column is expected to be unique. However, there are many use cases where the key column is not unique, and instead of overwriting existing values, we might want to collect the values into a List. This would provide more flexibility for handling cases where the column being used as the key is not guaranteed to be unique.

The method selectMapWithList() will allow us to use a non-unique column as a key like this,

Map<String, List<Author>> result = sqlSession.selectMap("selectAllAuthors", "firstName");

@harawata
Copy link
Member

Hello @NiazBinSiraj ,

There are many similar yet different use cases involving Map, like your recent PR #3235 .
And it is clearly not a viable solution to add a new set of methods to SqlSession for every requested use case.
As JDK provides flexible way to transform a list into map(s), users should embrace it instead of trying to modify MyBatis, IMHO.

If I understand this PR correctly, this answer may be helpful.
https://stackoverflow.com/a/78949985/1261766

Please let me know if I misunderstand something.

@NiazBinSiraj
Copy link
Author

NiazBinSiraj commented Sep 11, 2024

Hi @harawata ,

Thank you for your quick reply. And Yes! You're right, adding new methods to SqlSession for every use case isn't a viable solution. However, don't you think that, the existing selectMap() method should handle non-unique keys more effectively? I think, It should either return a Map<K, List<V>> for duplicate keys or throw an exception when duplicates are encountered. This behavior should also be clearly documented in the JavaDoc.

I'd love to hear your thoughts on this.

@harawata
Copy link
Member

@NiazBinSiraj ,

As I wrote, there are many similar but different use cases.
And everyone claims that their use case is the most popular one 😅
Personally, I would not entertain any idea that can be achieved by Stream + Collectors.

I actually want to remove the current selectMap because it encourages users to submit their version of selectMap (just to be clear, we won't remove it in 3.x).

@NiazBinSiraj
Copy link
Author

NiazBinSiraj commented Sep 11, 2024

@harawata,

I see your point now! 😅 In that case, I agree that the existing selectMap() isn't really necessary anymore, since we can achieve the same behavior with selectList() and a little stream magic.

selectList().stream()
          .collect(Collectors.toMap(
              RowData::getId,
              row -> row
          ));

Thanks for pointing that out! 🙌 It's definitely a good reminder not to encourage custom versions of selectMap(), or we'd be dealing with endless variations. 😅

@harawata
Copy link
Member

Exactly!
Thank you for your understanding and your contribution!

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

Successfully merging this pull request may close these issues.

2 participants