Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @EqualsAndHashCode With Include - Lombok

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; } 
like image 352
Sérgio Thiago Mendonça Avatar asked Jul 05 '19 18:07

Sérgio Thiago Mendonça


People also ask

What is Lombok EqualsAndHashCode?

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.

What is the use of @EqualsAndHashCode?

[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.

Does @data generate equals and hashCode?

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.

What is EqualsAndHashCode exclude?

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.


1 Answers

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; } 
like image 152
Mureinik Avatar answered Oct 09 '22 02:10

Mureinik