Using lomboks @Data and @Builder on entity

Beware of that data objects aren't entities! Simply put, there is problem with hashcode/equals (when it considers id fields) and also toString method with lazy loaded parts of entity. For reference you can check Vlad Mihalceas article.

You should:

  • exclude id fields from hashcode/equals
  • exclude association fields which are not managed in given entity from hashcode/equals
  • exclude all lazy loaded fields from toString method
  • exclude fields possibly causing circular references from toString method

For sure read at least something on topic of how does JPA do "dirty checking" before beeing confident that your handwritten or generated equals/hashcode method is ok.


try this code with lombok version 1.16.18 over :

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class User {
    private String id;
    private String firstName;
    private String lastName;
}

Tags:

Java

Lombok