Skip to content

Question: @Builder 관련 #28

@unanchoi

Description

@unanchoi
  • Builder를 사용할 때에 entity가 갖고 있는 필드 전부를 Builder를 만드는 생성자에 집어넣어야 하는지 궁금합니다!

  • ChatRoom.java

@Document(collection = "rooms")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChatRoom {

    @Id
    private String _id;

    private String roomName;

    @Nullable
    private List<ChatMessage> messages;

    private List<String> participantsId;

    @Builder
    public ChatRoom(String _id, String roomName, List<String> participantsId) {
        this._id = _id;
        this.roomName = roomName;
        this.participantsId = participantsId;
    }
}
  • ChatService에서 createChatRoom이라는 메소드를 만들고, chatRoom을 생성한 후에, chatRoom Repository에 저장하려고 하는 상황입니다.
    private final ChatRoomRepository chatRoomRepository;

    @Transactional
    public void createChatRoom(ChatRoomRegisterDto chatRoomRegisterDto) throws JsonProcessingException {

        ChatRoom chatRoom = ChatRoom.builder()
                ._id(chatRoomRegisterDto.get_id())
                .roomName(chatRoomRegisterDto.getRoomName())
                .participantsId(chatRoomRegisterDto.getParticipantsId());

    chatRoomRepository.save(chatRoom);

    }
  
  • message를 @nullable 로 지정해서, Builder에 집어넣지 않고, ChatRoom이 생성 가능할 줄 알았는데, 자꾸 에러가 발생해서, issue 남깁니다!

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions