In MyBatis 3, using foreach to iterate over a collection of Apache Commons Lang Pair objects can cause the loop variable item to become a String and trigger a “There is no getter for property named 'left'” exception. The root cause is not that Pair lacks getLeft, but that Pair implements Map.Entry, so MyBatis binds the left value (the key) to index and the right value (the value) to item.
If the input is List<Pair<String, String>> and the Pair's right value happens to be a string, a later access to #{pair.left} actually makes MyBatis resolve the left property of that string.
This is what the exception below really means:
There is no getter for property named 'left'
in 'class java.lang.String'
The issue is not that Pair#getLeft() violates the JavaBean specification; it is that by the time execution reaches this point, the variable named pair is no longer a Pair.
How to Reproduce the Issue
Suppose the Mapper receives a collection of user–role relationships:
int batchInsert(
@Param("pairs")
List<Pair<String, String>> pairs
);
Call it with:
List<Pair<String, String>> pairs = List.of(
Pair.of("1001", "admin"),
Pair.of("1002", "editor")
);
The XML reads the two values directly through left and right:
Intuitively, pair should be the following in each iteration:
Pair("1001", "admin")
Pair("1002", "editor")
But those are not the values MyBatis actually binds.
Apache Commons Pair Is Also a Map.Entry
The class used here is org.apache.commons.lang3.tuple.Pair. Besides providing getLeft() and getRight(), it implements Map.Entry<L, R>. The official Apache Commons Lang documentation also explicitly defines this mapping: the key is left, and the value is right.
Pair.left == Map.Entry.key
Pair.right == Map.Entry.value
These calls therefore come in two equivalent pairs:
pair.getLeft(); // 等价于 getKey()
pair.getRight(); // 等价于 getValue()
If Pair were passed to the property resolver as an ordinary object, both left and right would be readable. What actually changes the behavior is its status as a Map.Entry.
How MyBatis foreach Handles Map.Entry
The official MyBatis <foreach> documentation describes two binding modes:
- When iterating over a regular
Iterableor an array,indexis the current position anditemis the current element; - When iterating over a
Mapor a collection ofMap.Entryobjects,indexis the entry's key anditemis the entry's value.
The current processing logic in ForEachSqlNode can be simplified to this pseudocode:
This branch is what makes iteration over a Map more natural. For example:
<foreach collection="users" index="userId" item="user">
#{userId}, #{user.name}
</foreach>
When users is a Map<String, User>, the key goes into userId and the value goes into user.
The catch is that Apache Commons Pair also satisfies element instanceof Map.Entry.
Why item Becomes a String
Consider this data:
Pair.of("1001", "admin")
After it enters <foreach>, the variable bindings become:
| Value in Pair | Map.Entry Semantics | foreach Variable |
|---|---|---|
left = "1001" | entry.getKey() | index |
right = "admin" | entry.getValue() | item |
If the XML names the item variable pair, the full process is:
MyBatis then uses its property-access mechanism to look for a left getter on String. It cannot find one, so the exception includes class java.lang.String.
This type information is crucial. It shows that the current property-resolution target is the Pair's right value, not the Pair itself. If the right value is a Long, the exception may mention Long; if it is another business object, MyBatis will try to resolve the property against that object.
Option 1: Use index and item Directly
Since MyBatis has already split the Pair according to Map.Entry semantics, the smallest change is to use the two resulting variables directly:
At this point:
Pair.left → foreach.index → left
Pair.right → foreach.item → right
Note that index is no longer a List index such as 0, 1, or 2; it is the object returned by Map.Entry#getKey().
This approach works well when the change is small and Pair is only used temporarily near the Mapper. However, readers of the XML need to know MyBatis's special semantics for Map.Entry; otherwise, index="left" is still a little counterintuitive.
Option 2: Use an Explicit Parameter Object
If this data has a stable business meaning, I would prefer not to let Pair cross the Mapper boundary.
For example, a “user–role relationship” can be defined as an explicit parameter object:
Change the Mapper parameter to:
int batchInsert(
@Param("rows")
List<UserRoleRow> rows
);
The XML can go back to the common object-property form:
This not only avoids the Map.Entry branch, but also gives the parameter itself business semantics. userId and roleId are generally easier to understand than left and right, and are easier to maintain when field types or validation rules change.
If the calling code must keep Pair for now, convert it once before passing it to the Mapper:
List<UserRoleRow> rows = pairs.stream()
.map(pair -> new UserRoleRow(pair.getLeft(), pair.getRight()))
.toList();
Not Every Type Named Pair Triggers This
The criterion is not whether the type is called Pair, but whether the runtime element implements Map.Entry.
Pair, ImmutablePair, and MutablePair from Apache Commons Lang all take this branch because the latter two inherit from Pair. Tuple types from other libraries that do not implement Map.Entry are still bound to item as ordinary elements.
So, when troubleshooting a similar problem, it is more useful to confirm the actual element type and the interfaces it implements than to look only at the generic declaration.
Check the Actual Type First When You See a No-Getter Exception
The next time you see a similar exception:
There is no getter for property named 'xxx'
in 'class java.lang.String'
and the argument you pass is clearly a complex object, check the following in order:
- Start with the actual type in the exception rather than looking only at the Mapper method signature;
- Confirm what
itemandindexare bound to in<foreach>; - Check whether the collection elements implement
Map.Entry; - Only then decide whether the getter is missing or the property-resolution target has changed.
The key chain behind this issue can be reduced to one sentence:
Apache Commons Pair 实现 Map.Entry
→ MyBatis foreach 按 key/value 展开
→ right 被绑定为 item
→ pair.left 实际变成 String.left
So this is not a compatibility problem with Pair's getter. It is a subtle semantic conflict that arises when two perfectly reasonable interface designs overlap.