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

Argument name based constructor auto-mapping #2196

Merged

Conversation

harawata
Copy link
Member

@harawata harawata commented Mar 11, 2021

An attempt at fixing #2192

  • A new option argNameBasedConstructorAutoMapping is added.
  • When enabled, MyBatis searches a column that matches the constructor argument name.

Compared to the current (=column order based) constructor auto-mapping, name-based auto-mapping is more lenient.

  • The column order is irrelevant.
  • The number of columns can be greater than the number of arguments (but cannot be less).
  • The columns used in constructor auto-mapping are not used in property mapping. This prevents unintuitive behavior like Incorrect work with immutable objects #2207 .

Cc: @h3adache

When there are multiple constructors, users must use `@AutomapConstructor`.
Although it would be possible to implement find-the-best-match type logic,
# Conflicts:
#	src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
#	src/main/java/org/apache/ibatis/session/Configuration.java
#	src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java
#	src/test/java/org/apache/ibatis/builder/xsd/XmlConfigBuilderTest.java
@coveralls
Copy link

coveralls commented Sep 1, 2021

Coverage Status

Coverage increased (+0.03%) to 87.251% when pulling ba5cc2b on harawata:arg-name-based-constructor-automapping into b72067b on mybatis:master.

# Conflicts:
#	src/main/java/org/apache/ibatis/session/Configuration.java
#	src/site/es/xdoc/configuration.xml
#	src/site/ja/xdoc/configuration.xml
#	src/site/ko/xdoc/configuration.xml
#	src/site/xdoc/configuration.xml
#	src/site/zh/xdoc/configuration.xml
@harawata harawata force-pushed the arg-name-based-constructor-automapping branch from a9451fc to 7a76f77 Compare March 21, 2022 04:25
…perty-automapping

This logic is applied only to the new arg-name-based constructor auto-mapping.
Although it is possible (and it may also be the expected behavior) to apply the same logic to the old column-order-based constructor auto-mapping, it will break many existing solutions, probably.
@harawata
Copy link
Member Author

@kazuki43zoo @jeffgbutler @hazendaz

I plan to merge this soon.
Please let me know if you have any concern.
Thanks!

@@ -519,6 +525,10 @@ private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper
if (autoMapping == null) {
autoMapping = new ArrayList<>();
final List<String> unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix);
List<String> mappedInConstructorAutoMapping = constructorAutoMappingColumns.remove(mapKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harawata Might be useful for a comment here to understand why its removing vs others getting data. I think I grasp it but find it somewhat confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @hazendaz !
Added a brief comment.
The entry is used only once, so there is no point in keeping it on memory.

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.

3 participants