报错
org.springframework.data.mapping.MappingException:
No property b found on entity class com.xxx.A to bind constructor parameter to!
代码
@Data
@SuperBuilder
public class A extends Base {
private String a;
}
@Data
@SuperBuilder
public class Base {
private String common;
}
分析
public class A extends Base {
protected A(final A.ABuilder<?, ?> b) { // 发现 No property b 这个报错中的 b,推断构造函数有问题
super(b);
}
public static A.ABuilder<?, ?> builder() {
return new A.ABuilderImpl();
}
...
- 网上资料翻阅后找到关于 《@Builder和默认构造函数的冲突》,https://blog.csdn.net/weixin_43684257/article/details/117296134
解决
- 添加 @NoArgsConstructor 和 @AllArgsConstructor
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class A extends Base {
private String a;
}
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class Base {
private String common;
}
特殊情況
- 如果 B类 里面没有属性,只需要加 @NoArgsConstructor 即可,否则 build报错
@Data
@SuperBuilder
@NoArgsConstructor
public class B extends Base {
}
|