1

I used Builder pattern. This is Entity class.

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
public class Posts extends BaseTimeEntity{

    @Id
    @GeneratedValue
    private Long id;

    @Column(length = 500, nullable = false)
    private String title;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String content;

    private String author;

    @Builder
    public Posts(String title, String content, String author) {
        this.title = title;
        this.content = content;
        this.author = author;
    }
}

And This is DTO.

@Getter
@Setter
@NoArgsConstructor
public class PostsSaveRequestDto {

    private String title;
    private String content;
    private String author;

    public Posts toEntity() {
        return Posts.builder()
                .title(title)
                .content(content)
                .author(author)
                .build();
    }

}

I want to make update(title) query.

This is update part in controller.(not work!)

@PostMapping("/posts/update-title/{id}/title")
public Posts updateTitle(@PathVariable long id, @PathVariable String title) {   
    Posts p = postsRepository.getOne(id);
    p.builder().title(title).build();
    return p;
}

Is the Builder Pattern not available when updating(CRUD) in a spring boot? Can only be used to create objects(for constructor)? So, how to make UPDATE, using builder pattern(@builder)?

0 Answers0