awesomemypro
awesomemypro

Reputation: 561

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

Upvotes: 0

Views: 2544

Answers (2)

Francesc Recio
Francesc Recio

Reputation: 2235

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.

Upvotes: 0

Adya
Adya

Reputation: 1112

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.

Upvotes: 1

Related Questions