Why Lombok’s Getter/Setter Breaks MyBatis and EasyExcel: Hidden Pitfalls Explained
This article explores how Lombok’s auto‑generated getter/setter methods can conflict with MyBatis property naming conventions and cause null values, examines the underlying reflection logic in MyBatis, and also reveals why the @Accessor(chain=true) annotation disrupts EasyExcel’s CGLIB‑based mapping, offering practical solutions.
Preface
Last year we introduced the Lombok plugin, which eliminated boilerplate such as getters, setters, and toString, but we later discovered hidden pitfalls that affect other components.
Setter‑Getter Pitfall
Problem Discovery
Using Lombok’s @Data (which generates setter/getter methods) we encountered a MyBatis insert where the enum field nMetaType was always persisted as null.
Entity example:
@Data
public class NMetaVerify {
private NMetaType nMetaType;
private Long id;
// ... other fields
}All other fields were inserted correctly, but nMetaType remained null in the database.
Root Cause
Lombok generates getter/setter names that differ from the JavaBeans convention when the first letter is lowercase and the second letter is uppercase. MyBatis uses reflection to locate getter/setter methods, expecting the standard naming, so it fails to find the correct accessor.
Generated by Lombok:
public void setNMetaType(NMetaType nMetaType) { ... }
public NMetaType getNMetaType() { ... }Standard JavaBeans (used by MyBatis, IDEs, and the JDK):
public NMetaType getnMetaType() { ... }
public void setnMetaType(NMetaType nMetaType) { ... }MyBatis’s PropertyNamer.methodToProperty logic shows how it derives property names from method prefixes and handles the case where the second character is uppercase.
String methodToProperty(String name) {
// ... logic that lower‑cases the first character unless the second is uppercase
}A test confirms the conversion results:
Method name: isName Property name: name
Method name: getName Property name: name
Method name: getnMetaType Property name: nMetaType
Method name: getNMetaType Property name: NMetaType@Accessor(chain = true) Issue
Problem Discovery
When exporting data with EasyExcel, newly added entities annotated with @Accessor(chain = true) failed to map correctly.
We use chainable setters like:
new UserDto()
.setUserName("")
.setAge(10)
.setBirthday(new Date());Root Cause
EasyExcel relies on CGLIB’s BeanMap, which in turn uses the JDK’s Introspector. The introspector only recognizes setter methods that return void. The chained setters generated by Lombok return the object itself, so they are ignored, leading to missing property values.
Solution
Remove the @Accessor(chain = true) annotation from the entity.
Alternatively, wait for EasyExcel to switch to a reflection library that supports non‑void setters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
