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

Constructor auto mapping could fail when columnPrefix is specified in the parent resultMap #1495

Closed
hiirage opened this issue Mar 7, 2019 · 4 comments
Assignees
Labels
Milestone

Comments

@hiirage
Copy link

hiirage commented Mar 7, 2019

MyBatis version

3.5.0

Database vendor and version

H2

Test case or example project

contructor_mapping.zip

Steps to reproduce

Run FooMapperTest to find one object Foo with an immutable Bar from DB

Expected result

get a Foo with a Bar constructed by autoMapping with columnPrefix "b_"

Actual result

An exception thrown with message:
...JdbcSQLSyntaxErrorException: Column "b_B_STR" not found ...

@harawata
Copy link
Member

harawata commented Mar 9, 2019

Thank you for the report & repro, @hiirage !

There seems to be a few issues indeed and I will try to fix them, but you should know that columnPrefix is not used in constructor auto-mapping.
Constructor auto-mapping is performed using constructor signature (i.e. the number and the types of arguments).

In your demo, for example, Bar's constructor takes two arguments String and Integer.
MyBatis checks the returned columns in the specified order and use the first two values if they can be converted to String and Integer respectively.
In other words, constructor auto-mapping relies on the column order of your query.

Personally, I recommend explicit mapping for constructor.

@harawata harawata added the bug label Mar 9, 2019
@harawata harawata self-assigned this Mar 9, 2019
@harawata harawata added this to the 3.5.1 milestone Mar 9, 2019
@harawata harawata changed the title Constructor auto mapping failed using @AutomapConstructor Constructor auto mapping could fail when columnPrefix is specified in the parent resultMap Mar 10, 2019
@harawata
Copy link
Member

@hiirage ,

Both this issue and #1496 should be fixed in the latest 3.5.1-SNAPSHOT.
Let us know if you find any issue!

p.s.
Another issue I noticed is that the columns used for constructor auto-mapping are used again in applyAutomaticMappings().
This means that the values are set to the same properties twice (first via constructor, then via setter (or directly if it's 'final')).
There also is a corner case that causes unintentional mappings.
The failing test is here.

@h3adache
Copy link
Member

That’s a known issue @harawata. I thought we talked about it last time. I can’t remember if we had said it was necessary for that 2nd level mapping for some reason.

@harawata
Copy link
Member

Thank you for the comment, @h3adache !
I must have missed the point and didn't realize the 'same property is set twice' part, then.
If it's known, that should be fine. :)

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

No branches or pull requests

3 participants