Org.apache.commons.lang3.tuple.Pair as an update parameter, SQL in XML takes no value, error

Source: Internet
Author: User
Tags reflector wrapper

Project with the Mybatis, change a requirement today, landing implementation is a batch update, and only need to update a field (name) based on the primary key (ID).

So, without hesitation, design a data structure like this:

    • Since it is a batch update, the outer layer must be a List
    • Each element in the list contains only ID & name, so the Org.apache.commons.lang3.tuple.Pair is chosen to encapsulate the data (just don't want to write another do or VO or MO)
    • The final data structure is: List<pair<integer, string>>

The SQL statement in XML is simple, as follows:

    <UpdateId="Updatebyapachecommonspair">        <foreach Collection="List" separator=";" Item="X">            UPDATEEmployeeSETName=#{x. Right}WHEREId=#{x. Left}        </Foreach>    </Update>

Tip: Sql in XML introduces a variable named X (which is used later)

All goes well, but when testing, the error is as follows:

org.apache.ibatis.exceptions.PersistenceException: # # # Error updating database. Cause:org.apache.ibatis.reflection.ReflectionException:There is no Getter forProperty named "Right" in ' class Java.lang.String '# # # The error may involve defaultparametermap### the error occurred whilesetting parameters### sql:update employee SET name= ? WHERE id =?# # Cause:org.apache.ibatis.reflection.ReflectionException:There is no getter forProperty named "Right" in ' class Java.lang.String 'At org.apache.ibatis.exceptions.ExceptionFactory.wrapException (Exceptionfactory.java:30) at Org.apache.ibatis.session.defaults.DefaultSqlSession.update (Defaultsqlsession.java:200) at Org.apache.ibatis.binding.MapperMethod.execute (Mappermethod.java:62) at Org.apache.ibatis.binding.MapperProxy.invoke (Mapperproxy.java:53) at Com.sun.proxy. $Proxy 4.updateByApacheCommonsPair (Unknown Source) at Com.atguigu.mybatis.test.MyBatisTest.te Stupdatebyapachecommonspair (Mybatistest.java:42) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (Nativemethodaccessorimpl.java:62) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:43) at Java.lang.reflect.Method.invoke (Method.java:498) at org.junit.runners.model.frameworkmethod$1.runReflectiveCall (frameworkmethod.java:50) at Org.junit.internal.runners.model.ReflectiveCallable.run (Reflectivecallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively (Frameworkmethod.java:47) at Org.junit.internal.runners.statements.InvokeMethod.evaluate (Invokemethod.java:17) at Org.junit.runners.ParentRunner.runLeaf (Parentrunner.java:325) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (Blockjunit4classrunner.java:78) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (Blockjunit4classrunner.java:57) at org.junit.runners.parentrunner$3.run (parentrunner.java:290) at org.junit.runners.parentrunner$1.schedule (parentrunner.java:71) at Org.junit.runners.ParentRunner.runChildren (Parentrunner.java:288) at org.junit.runners.parentrunner.access$(parentrunner.java:58) at org.junit.runners.parentrunner$2.evaluate (parentrunner.java:268) at Org.junit.runners.ParentRunner.run (Parentrunner.java:363) at Org.junit.runner.JUnitCore.run (Junitcore.java:137) at Com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs (Junit4ideatestrunner.java:68) at Com.intellij.rt.execution.junit.ideatestrunner$repeater.startrunnerwithargs (IdeaTestRunner.java:47) at Com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart (Junitstarter.java:242) at Com.intellij.rt.execution.junit.JUnitStarter.main (Junitstarter.java:70) caused By:org.apache.ibatis.reflection.ReflectionException:There is no getter forProperty named "Right" in ' class Java.lang.String 'At Org.apache.ibatis.reflection.Reflector.getGetInvoker (Reflector.java:409) at Org.apache.ibatis.reflection.MetaClass.getGetInvoker (Metaclass.java:164) at Org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty (Beanwrapper.java:162) at Org.apache.ibatis.reflection.wrapper.BeanWrapper.get (Beanwrapper.java:49) at Org.apache.ibatis.reflection.MetaObject.getValue (Metaobject.java:122) at Org.apache.ibatis.reflection.MetaObject.getValue (Metaobject.java:119) at Org.apache.ibatis.mapping.BoundSql.getAdditionalParameter (Boundsql.java:75) at Org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters (Defaultparameterhandler.java :72) at Org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize ( Preparedstatementhandler.java:93) at Org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize (Routingstatementhandler.java: 64) at Org.apache.ibatis.executor.SimpleExecutor.prepareStatement (Simpleexecutor.java:86) at Org.apache.ibatis.executor.SimpleExecutor.doUpdate (Simpleexecutor.java:49) at Org.apache.ibatis.executor.BaseExecutor.update (Baseexecutor.java:117) at Org.apache.ibatis.executor.CachingExecutor.update (Cachingexecutor.java:76) at Org.apache.ibatis.session.defaults.DefaultSqlSession.update (Defaultsqlsession.java:198)    ... More

The problem is already out, then solve it.

The information in the error message is still easier to read: Getter method with no right field in the Java.lang.String class

Strange, how can you report such a mistake:

    • There is really no right field in the Java.lang.String class
    • The right field is in pair (because I use a pair)
    • Are they all stirred together?

Guess some reasons, but all wrong, finally, there is no way, only to write a Cyhpair (also a two-field pojo) to achieve, the result is also possible.

So, then there is the idea: Compare your own written cyhpair and Org.apache.commons.lang3.tuple.Pair in which places are different.

Finally found (experiment for a long time), a big difference is that the Org.apache.commons.lang3.tuple.Pair implementation of the Map.entry interface (Cyhpair did not write their own), this will not affect?

The final conclusion proves that: it is because of the implementation of the map.entry so that the latter can not get the value , so the error is as above.

But why is this? Then, continue the source debugging.

Demo Code Address: Https://github.com/cyhbyw/mybatis_atguigu

Demo Project Name: Mybatis_cyh_test_mapentry

First: First to debug the normal case (that is, based on the Cyhpair to do the update operation)

01. As you can see, line#73 determines whether it belongs to Map.entry; Since my cyhpair does not implement it, the code goes into the Else section; Notice that the second argument here is O itself, and O is an instance of Cyhpair.

02. From line#102 you can see that the O Cyhpair object is bound to X (hint: x is a variable in SQL)

Simple conclusion:x This variable is cyhpair type !

Second step: Next debugging is not normal (that is, based on Org.apache.commons.lang3.tuple.Pair to do the update operation)

01. Still the judgment of line#73, but this time, the judgment is established, so the code goes to line#77 line; Note The second parameter is Mapentry.getvalue (), and this value is the string Apachecommonspair;

02. When binding, the Apachecommonspair string value is bound to the variable x

03. So it is now easy to understand why it error "getter method with no right field in the Java.lang.String class", because it is 02 steps to bind the string Apachecommonspair to X, then there is definitely no right word There is no corresponding getter method for the paragraph ~ ~

Org.apache.commons.lang3.tuple.Pair as an update parameter, SQL in XML takes no value, error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.