简体   繁体   中英

How to access Builder() of a JPA entity using Lombok with Spring Boot?

This is my entity class. I have used @Builder annotation. The Lombok version is 1.16.16

@Builder
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
@Data
@Entity
@EqualsAndHashCode
@ToString
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String author;

}

Now, when I am trying to call the Builder() method in the main class, it is not getting resolved.

Stream.of("Post one", "Post two").forEach(
                title -> this.booksList.save(Book.Builder().title(title).author("author of " + title).build())
            );   

This is being shown.

The method Builder() is undefined for the type Book

Try by Book b = new Book.builder() .id(id).name(name).author("author of " + title).build();

May be you are trying to use title, which you haven't defined.

Your fields are:

private Long id
private String name
private String author

and in your builder constructor you put Book.Builder().title(title) , the title is missing in the Book entity, a part "builder()" has to start with lowercase.

EDIT:

Ok, then you can remove @AllArgsConstructor, @NoArgsConstructor, @RequiredArgsConstructor and test it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM