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

Offer configuration to disable autoMappingsCache in DefaultResultSetHandler #1968

Closed
GitHanter opened this issue Jul 2, 2020 · 3 comments
Closed

Comments

@GitHanter
Copy link

GitHanter commented Jul 2, 2020

MyBatis version

3.5.3

Database vendor and version

SQL Server 2012

Test case or example project

<select id="selectMultiResultSet" resultMap="AMap,BMap,BMap">

BMap javaType has a property aProperty

The ResultSets of the two BMap is different (with different columns), e.g

The first BMap ResultSet (BResultSet1) is
columnA, columnB

The second BMap ResultSet (BResultSet2) is
columnb, columnC

When the DefaultResultSetHandler encounter the BResultSet1, it'll cache the aProperty on the autoMappingsCache,
When the DefaultResultSetHandler handle the BResultSet2 for the second BMap, it'll use the aProperty to retrieve value from BResultSet2, which doesn't exit, and JDBC Driver will complain The column name aProperty is not valid.

Possible fix

We can add a configuration attribute to

<resultMap useAutoMappingsCache="true(Default)/false">
...
</resultMap>

And change method createAutomaticMappings in DefaultResultSetHandler a little bit like

private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
    final String mapKey = resultMap.getId() + ":" + columnPrefix;
    List<UnMappedColumnAutoMapping> autoMapping = null;
    if (resultMap.isUseAutoMappingsCache()) {
           autoMapping = autoMappingsCache.get(mapKey);
    }

Demo test on PR92

GitHanter added a commit to GitHanter/mybatis-3 that referenced this issue Jul 18, 2020
@harawata
Copy link
Member

Hello @GitHanter ,

Thanks for the demo project!
It really helps. :)

I was aware that there might be issues like this caused by the auto-mapping cache.
But I think it's easily worked around by adding a new result map which extends the original one.

<resultMap id="multiResultSetMap" type="test.User" autoMapping="true">
  <id column="id" />
  <result property="name" column="name" />
</resultMap>

<resultMap id="multiResultSetMapCopy" type="test.User" extends="multiResultSetMap">
</resultMap>

<select id="getMultipleResultSet"
  resultMap="anotherEntityMap,multiResultSetMap,multiResultSetMapCopy">
..

Compared to the proposed option, this approach seems better because you won't lose the benefit of auto-mapping cache.
What do you think?

@GitHanter
Copy link
Author

GitHanter commented Jul 19, 2020

Yes, this work around can solve this problem, but some of our legacy project has four to five ResultSets mapped to the same ResultMap, and it'll be nice to have this configuration to turn the automapping cache off.

Anyway, you are right, this can solve the issue and use the automapping cahe to maximize performance. Thanks.

@harawata
Copy link
Member

Closing for now. We might reconsider if this turned out to be a frequently requested feature.

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