SoFunction
Updated on 2025-03-03

The problem of using Lombok subclass to inherit the parent class, and the parent class attribute does not take effect and the problem of solving the problem of

Use Lombok subclass to inherit the parent class, and the parent class attribute does not take effect

Subclass addition:

@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class SysRights extends BaseEntity {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String rightsId;
    private String rightsName;
    private String rightsUrl;
    private String rightsIcon;
}

Problem solved~~~~

What should be paid attention to when inheriting Lombok

The lombok project was created to save us the trouble of manually creating basic methods such as getters and setters. It can automatically generate methods such as getters and setters when we compile the source code.

That is, the effect it can achieve is:

There are no getter and setter methods in the source code, but there are getter and setter methods in the compiled bytecode file.

@Data during inheritance

We know that @Data annotation is in the class, which annotates the convenient methods of @ToString, @EqualsAndHashCode, @Getter for all fields of the class, and annotates @Setter for all non-final fields.

Note that the @EqualsAndHashCode annotation and the @ToString annotation ignore the member variables of the parent class by default. The test code is as follows:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class School {
 
  public int id;
 
  public String name;
 
  public String age;
 
}
 
@Data
class ShangHai extends School {
  private String type;
}
 
 
class test {
 
  public static void main(String[] args) {
    ShangHai shangHai = new ShangHai();
    (1);
    ("Shanghai University");
    ("A");
 
    ShangHai shangHai2 = new ShangHai();
    (2);
    ("East China Normal University");
    ("A");
 
    ((shangHai2));
    (());
    (());
 
  }
}

Its printing results are:

true
ShangHai(type=A)
ShangHai(type=A)

It can be found here that the above two instances do not compare whether the member variables of the parent class are the same, and only compare the values ​​of the type field, so it is true. Similarly, the toString method will not print members of the parent class.

Involve parent class member variables in logic

This solution is very simple, you just need to put a parameter on the annotation:

@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
class ShangHai extends School {
  private String type;
}

Print results

true
Shanghai(super=School(id=1, name=Shanghai University, age=null), type=A)
Shanghai(super=School(id=2, name=East China Normal University, age=null), type=A)

Notice:

From the above code demonstration, we can know that when class inheritance, we should pay attention to the @Data annotation that will not involve the members of the parent class, and we need to add the parameter calledSuper = true.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.