Why Lombok’s Getter/Setter Breaks MyBatis and EasyExcel – Causes and Fixes
This article explains how Lombok’s automatic getter/setter generation can cause MyBatis to store enum fields as null, details the underlying JavaBeans naming conflict, and also reveals why EasyExcel fails with @Accessor(chain=true) annotations, offering practical solutions for both problems.
When using Lombok in a Java project, the generated getter/setter methods can clash with frameworks that rely on the JavaBeans naming convention.
Setter-Getter Method Pitfall
Problem Discovery
When inserting an entity with MyBatis, the enum field nMetaType was always stored as null, even though the object contained a value.
@Data
public class NMetaVerify {
private NMetaType nMetaType;
private Long id;
// ... other fields
}Debugging showed the field had data before the insert, but MyBatis obtained the property name via reflection using the getter method, which Lombok generated as getNMetaType (capital N), while MyBatis expects getnMetaType.
Cause
Lombok follows a rule where a property whose first two letters are lowercase‑uppercase (e.g., nMetaType) gets getters/setters with the second letter capitalized, diverging from the JavaBeans convention used by MyBatis, IDEs, and the official specification.
// Lombok‑generated
public NMetaType getNMetaType() { ... }
public void setNMetaType(NMetaType nMetaType) { ... } // Standard JavaBeans
public NMetaType getnMetaType() { return nMetaType; }
public void setnMetaType(NMetaType nMetaType) { this.nMetaType = nMetaType; }The MyBatis PropertyNamer.methodToProperty logic (excerpt) shows how it derives the property name from method names.
public static String methodToProperty(String name) {
if (name.startsWith("is")) { name = name.substring(2); }
else if (name.startsWith("get") || name.startsWith("set")) { name = name.substring(3); }
// ... handling of single‑letter names and lower‑casing ...
return name;
}Solution
Rename the field so that the first two letters are both lowercase (e.g., nmMetaType) or avoid the special case.
Manually write proper getters/setters for such fields, or let the IDE generate them instead of Lombok.
@Accessor(chain = true) Annotation Issue
Problem Discovery
When exporting objects with EasyExcel, newly added DTOs annotated with @Accessor(chain = true) failed to export correctly.
new UserDto()
.setUserName("")
.setAge(10)
.setBirthday(new Date());Cause
EasyExcel relies on CGLIB and Java’s Introspector to discover bean properties. The introspection code only treats methods that return void as setters, so chainable setters that return the object break the detection.
// CGLIB BeanMap usage
BeanMap.create(resultModel).putAll(map);
// Introspector snippet
if (void.class.equals(resultType) && name.startsWith(SET_PREFIX)) {
pd = new PropertyDescriptor(beanClass, name.substring(3), null, method);
}Solution
Remove the @Accessor(chain = true) annotation.
Await an update from EasyExcel to support non‑void returning setters, or replace the underlying CGLIB usage.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
