EqualsAndHashCode.Exclude 无法解析为类型

EqualsAndHashCode.Exclude cannot be resolved to a type

我将 Project Lombok 与 Eclipse 一起使用,我可以编译以下内容 class。

现在我想通过使用像 shown in the Lombok's documentation.

这样的注释 @EqualsAndHashCode.Exclude 从 hash 和 equals 方法中排除一个字段

不幸的是,这无法编译。为什么?

@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
public class Foo extends Bar implements Serializable {

  private static final long serialVersionUID = 36364364363634634;

  @Id
  @GenericGenerator(name = "PKGenerator", strategy = "com.project.utils.PrimaryKeyGenerator")
  @GeneratedValue(generator = "PKGenerator")
  @Column(name = "guid")
  @EqualsAndHashCode.Exclude
  private String id;

  @NotNull
  private String code;

  //...other fields omitted
}

我得到的编译错误是:

EqualsAndHashCode.Exclude cannot be resolved to a type

不管注释应用于哪些字段以及使用callSuper=truefalse

环境是:

Version: Oxygen.3a Release (4.7.3a)
..
Lombok v1.18.0 "Envious Ferret" is installed.

您是否也更新了构建文件中的依赖项?

TL;DR: 您需要 Lombok 1.16.22 或更高版本,但您真正使用的是哪个 Lombok? (见下文)


我发现问题在于 Lombok Eclipse 插件使用的 Lombok 版本(最新)与 Maven 依赖项之一(较旧)不匹配。

如果你愿意,spring 引导到 "blame",因为项目是使用 start.spring.io 设置的,并且依赖项 spring-boot-starter-web 设置了 lombok 版本 属性 至撰写本文时的 1.16.20。

在这种情况下,有效 pom 中的版本胜过 IDE 插件的版本,我们可以从 The Lombok documentation:

中读到

Prior to lombok 1.16.22, inclusion/exclusion could be done with the of and exclude parameters of the @EqualsAndHashCode annotation. This old-style inclusion mechanism is still supported but will be deprecated in the future.