How to use @EqualsAndHashCode
With Include
, Lombok library for Java.
@EqualsAndHashCode.Include( )
How to make Equals compare the class id?
Example:
@EqualsAndHashCode.Include( ) @Table(name = "USER") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "IDENTITY_USER") private Long identity; }
equalsAndHashCode. callSuper config key. NEW in Lombok 0.10: Unless your class is final and extends java. lang. Object , lombok generates a canEqual method which means JPA proxies can still be equal to their base class, but subclasses that add new state don't break the equals contract.
[Java] Annotation Type EqualsAndHashCode. Class annotation used to assist in creating appropriate equals() and hashCode() methods. The @EqualsAndHashCode annotation instructs the compiler to execute an AST transformation which adds the necessary equals and hashCode methods to the class.
The annotation @Data with inheritance produces the next warning: Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java. lang.
The @EqualsAndHashCode.Exclude annotation marks a field so that Lombok doesn't use that field when generating equals and hashCode: @EqualsAndHashCode public class Employee { private String name; @EqualsAndHashCode.
The Include
annotation is used on the member(s) you want to include in the equals
and hashCode
methods. If you want to specify exactly which members should be used (instead of the default of all non-static non-transient members), you could use the onlyExplicitlyIncluded = true
option in the @EqualsAndHashCode
annotation:
@EqualsAndHashCode(onlyExplicitlyIncluded = true) @Table(name = "USER") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "IDENTITY_USER") @EqualsAndHashCode.Include private Long identity; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With