継承があるオブジェクトでは、@Data を使うと警告
Generating equals/hashCode implementation but without a call to superclass,
even though this class does not extend java.lang.Object.
If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
@EqualsAndHashCodeを書かずにいるとビルド時に警告されるのですが、デフォルトはfalse で
callSuper=false は、super.equals(Object) を呼び出さずに equals を自作する必要性を意味します。
一般には、@EqualsAndHashCode(callSuper=true) を使うことが多いでしょう。
継承元クラスのフィールドも toString() で出力させるなら、@ToString(callSuper=true)
サンプル
@Data public class Fruit{ private int basePrice; }
@Data @EqualsAndHashCode(callSuper=true) @ToString(callSuper=true) public class Apple extends Fruit{ private String name; pricate String barcode; }
equals で対象外にしたいフィールドがある場合は、exclude を@EqualsAndHashCodeで指定します。
@Data @EqualsAndHashCode(callSuper=true, exclude={"barcode"}) @ToString(callSuper=true) public class Apple extends Fruit{ private String name; private String barcode; }
exclude = { } で複数指定できます。