简体   繁体   English

如何使用带有Spring Boot的Lombok访问JPA实体的Builder()?

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

This is my entity class. 这是我的实体类。 I have used @Builder annotation. 我使用过@Builder注释。 The Lombok version is 1.16.16 Lombok版本是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. 现在,当我尝试在主类中调用Builder()方法时,它没有得到解决。

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 对于类型Book,方法Builder()未定义

Try by Book b = new Book.builder() .id(id).name(name).author("author of " + title).build(); 尝试书籍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. 并且在构建器构造函数中放入Book.Builder().title(title) ,Book实体中缺少标题 ,“builder()”部分必须以小写开头。

EDIT: 编辑:

Ok, then you can remove @AllArgsConstructor, @NoArgsConstructor, @RequiredArgsConstructor and test it. 好的,那么你可以删除@AllArgsConstructor, @NoArgsConstructor, @RequiredArgsConstructor并测试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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