【问题标题】:Lombok Annotations are not working with inheritanceLombok 注解不适用于继承
【发布时间】:2025-12-11 16:50:01
【问题描述】:

我有类似父类的东西 (File A.java)。
每个班级都在扩展这个班级,因为每个班级都需要name

当我在File X.java 中使用@With 函数时,它适用于.withDescription("xxx"),它是File B.java 的一部分。
但是不能将@With 函数与.withName("xxx"); 一起使用
至少 IntelliJ 没有显示 .withName("xxx")
但是继承的.setName("xxx") 工作正常。

知道如何让它按预期工作吗? 我见过this Answer 使用@SuperBuilder,但我想在没有@Builer / @SuperBuilder 的情况下这样做

文件 A.java

@Getter
@Setter
@With
@MappedSuperclass
public class A {
  public String name;
}

文件 B.java

@Getter
@Setter
@With
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class B extends A {
  public String description;
}

文件 X.java

public class X {
  public void x() {
    var b = new B().withDescription("xxx"); // it works
    b.withName("xxx"); // is NOT working.
    b.setName("xxx"); // only set is working, even it's only inherited.
}

【问题讨论】:

    标签: java inheritance annotations lombok intellij-lombok-plugin


    【解决方案1】:

    您需要将@AllArgsConstructor添加到A

    【讨论】:

    • 我以为我试过了,但看起来我没有。但在 A 中我也有:@Setter(AccessLevel.NONE) @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; 和 @AllArgsConstructor 我也可以设置 withId,我不想这样做,这就是我有 @Setter(AccessLevel.NONE) 的原因。有什么想法可以避开这个拉敏吗?
    • 除了你不想使用的 Builder,我什么都没有想到。