0

I have written a query in myBatis as below. The issue in the below query is that. The value of col3 is the last value inserted in the Map which is passed to the query. It is not the value for the corresponding key.

<update id="upd10"  parameterType="map">
 UPDATE TABLE_NAME SET col1 = #{col1_value}, col3= {item.value} WHERE 
 col2 IN 
    <foreach item="item" index="index" collection="dateMap.entrySet()" open="("        separator="," close=")">
        #{item.key}
    </foreach>
</update>

My Scenario is as below,

If the value of col2 is same as the item.key in the hashmap which i am passing to the query then i want to set the value of col3 to item.value.

mike
  • 11
  • 1
  • 4
  • If the exception you are getting is something about the TypeHandler for the value, then your solution is right there: http://stackoverflow.com/questions/35717551/mybatis-error-on-collection-of-map-entry-and-map-entryset – Florian Schaetz Mar 04 '16 at 06:37
  • Thanks Florian i was able to get the Query working. But now the value of the col3 is not the value for col2 key. The value of the col2 is the last value in the map. – mike Mar 04 '16 at 07:15
  • I'm surprised that the "item.value" thing outside the foreach works at all, to be honest. Do you have an "item" in your map? Personally, I doubt that your query will ever work this way, since the value you need to put into it depends on which element results in the IN part to succeed - but you have no chance of knowing which did. I suspect you will need to iterate over your map manually and do the for every entry in your map seperately. – Florian Schaetz Mar 04 '16 at 07:28
  • What is happening is that it picks up the last inserted value in the Map and assigns it to the Column. So it is not referring to the item.key. Is it possible do this way. Or as you are saying we need to do it separately ONLY. – mike Mar 04 '16 at 10:21
  • Example: You have map entries... (1,A), (2,B), (3,C). No you do a an update like this... `UPDATE ...SET... = (A or B or C, depending...) WHEN ... IN (1,2,3)`. How do you expect MyBatis to know, WHICH value to use? The test for IN will run in the database and that only tells you "Yes, one of these three values fits" - but not which one. So, this construct can never work, since you have two levels: MyBatis must decide which value to use, but it cannot, since only the database knows which key was found. – Florian Schaetz Mar 04 '16 at 11:12

0 Answers0