Using lomboks @Data and @Builder for an entity

I am using the following:

@Entity
@Data
@Builder
@NoArgsConstructor(force = true)
public class User {
    private String id;
    private String firstName;
    private String lastName;
}

what I want to achieve: to use JPA I need a POJO with noArgConstructor, getters / seters and equals / hashCode / toString.

To create an instance (for example, in tests) I want to use User.builder (). build ();

Problem: it does not compile, it seems that the problem is related to NoArgConstructor and RequiredFieldsConstructor:

Error:(15, 1) java: constructor User in class x.y.z.User cannot be applied to given types;
required: no arguments
found:    java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String
reason: actual and formal argument lists differ in length

Update: an error occurs when I try to create a new object using new... builder () works.

What am I missing? Is it impossible to use @Data, @Entity and @Builder at the same time?

+4
source share
3 answers

I will answer my question by discussing comments.

, @RoelSpilker, Builder Data Pojo, AllArgs- NoArgs:

 @RequiredArgsConstructor
 @NoArgsConstructor
 @Data
 @Builder
 public class Person {...}

: , , . ( ) , /.

0

lombok 1.16.18:

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

Lombok : Person.builder(). Name ( "Adam Savage" ). City ( "San Francisco" ). Job ( "Mythbusters" ). Job ( "Unchained Reaction" ). Build();

? , , ?

+1
source

Source: https://habr.com/ru/post/1660344/


All Articles