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

avoid unexpected automapping #895

Merged
merged 2 commits into from
Jan 17, 2017

Conversation

dulong
Copy link

@dulong dulong commented Jan 13, 2017

avoid unexpected automapping when a explicit mapped proerty has the same name as another column

@harawata
Copy link
Member

Could you provide a test case?

@dulong
Copy link
Author

dulong commented Jan 17, 2017

@harawata Hi, I added a test case to the existed auto mapping test suite

@harawata harawata self-assigned this Jan 17, 2017
@harawata harawata added this to the 3.4.3 milestone Jan 17, 2017
@harawata harawata merged commit 978b922 into mybatis:master Jan 17, 2017
@harawata
Copy link
Member

Thank you, @dulong ! =D

@kazuki43zoo kazuki43zoo added enhancement Improve a feature or add a new feature and removed waiting for feedback labels Jan 17, 2017
@kazuki43zoo
Copy link
Member

@harawata , Is this enhancement or bug ?

@harawata
Copy link
Member

A bug, I would say.

@kazuki43zoo kazuki43zoo added bug and removed enhancement Improve a feature or add a new feature labels Jan 17, 2017
@kazuki43zoo
Copy link
Member

kazuki43zoo commented Jan 17, 2017

Do not need a warning log in this case ?

@@ -477,7 +477,7 @@ private Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject
}
}
final String property = metaObject.findProperty(propertyName, configuration.isMapUnderscoreToCamelCase());
if (property != null && metaObject.hasSetter(property)) {
if (property != null && metaObject.hasSetter(property) && !resultMap.getMappedProperties().contains(property)) {
Copy link
Member

@kazuki43zoo kazuki43zoo Jan 17, 2017

Choose a reason for hiding this comment

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

I think it should not use && condition in this case.

Because...
If resultMap.getMappedProperties().contains(property) is true, execute following code.

configuration.getAutoMappingUnknownColumnBehavior()
        .doAction(mappedStatement, columnName, (property != null) ? property : propertyName, null);

In this case, i think it is already mapped property rather than not unknown column.

Copy link
Member

Choose a reason for hiding this comment

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

I might not fully understand the expected behavior of autoMappingUnknownColumnBehavior.
Feel free to commit the necessary changes :)

Copy link
Member

Choose a reason for hiding this comment

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

I might not fully understand the expected behavior of autoMappingUnknownColumnBehavior.

See #585.

I will improve a contributing code.

Copy link
Member

Choose a reason for hiding this comment

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

OK..I have to admit that I am a little bit confused 😆
Please let me clarify.

When running the contributed test case (AutomappingTest.shouldGetAUserWhithPhoneNumber()), four columns (id, name, phone and phone_number) are returned from the query and only three of them are mapped.

  • 'phone_number' is explicitly mapped
  • 'id' and 'name' are auto-mapped

I thought that the unmapped column 'phone' is an unknown column, but it actually isn't, is that correct?

Copy link
Member

@kazuki43zoo kazuki43zoo Jan 18, 2017

Choose a reason for hiding this comment

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

Hi @harawata

In My opinion, the phone is valid column name as auto-mapping (if does not use explicit mapping...), it skip because it has been mapped by explicit mapping already. Hence i think phone is not unknown column.
I think we should consider to apply a mechanism that notify duplicate mapping for same property (like same as autoMappingUnknownColumnBehavior).

for example:

  • NONE: skip auto-mapping
  • WARNING: skip auto-mapping and output warning log that notify duplicate mapping for same property
  • FAILING : throw exception that that notify duplicate mapping for same property

What do you think ?

Copy link
Member

Choose a reason for hiding this comment

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

Hence i think phone is not unknown column.

Okay, I have no strong opinion about this.

I think we should consider to apply a mechanism that notify duplicate mapping for same property (like same as autoMappingUnknownColumnBehavior).

The duplicate mapping situation like this would be rare.
And this unknown column detection can be inaccurate [1] anyway, so let's consider when someone desperately wants it ;)

[1] There will be false-positives with a nested result map.

Copy link
Member

Choose a reason for hiding this comment

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

let's consider when someone desperately wants it ;)

Okay :)

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this pull request Jan 17, 2017
@kazuki43zoo
Copy link
Member

Hi @harawata and @dulong,
I've improved contributing code via a5895f7. If my change has problem, please post comment.

Thanks.

@dulong
Copy link
Author

dulong commented Jan 18, 2017

Hi @kazuki43zoo , thanks for the improvement, i appreciated that 👍

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

Successfully merging this pull request may close these issues.

3 participants