Spring Data中的@OneToMany(带有消息列表的频道,渴望获取)

问题描述 投票:2回答:1

我想正确地将消息添加到频道。

没有消息的频道正确地添加到数据库:

{
    id: 1,
    name: "Off-top",
    messageDTOs: [ ],
    participantDTOs: [
    {
        id: 1,
        firstName: "Szef",
        lastName: "Ceo",
        email: "[email protected]",
    role: "ADMIN",
    manager: true
    }
}

但是,如果我向通道添加一条消息,我会收到GET:/ channels的HTTP错误500。

我用FetchType.EAGER和相关的课程如下:

message.Java:

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Message {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(nullable = false)
    private String content;

    @OneToOne
    private Employee author;

    @Column(nullable = false)
    private LocalDateTime creationTime;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "channel_id")
    private Channel channel;

    public Message(String content, Employee author, Channel channel) {
        this.content = content;
        this.author = author;
        this.creationTime = LocalDateTime.now();
        this.channel = channel;
    }

    public boolean checkIfDataEquals(Message message) {
        return content.equals(message.getContent()) &&
                author.checkIfDataEquals(message.getAuthor()) &&
                channel.checkIfDataEquals(message.getChannel());
    }
}

channel.Java

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Channel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    @OneToMany(mappedBy = "channel", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Message> messages;

    @Column(nullable = false)
    @ManyToMany
    private List<Employee> participants;

    public Channel(String name, List<Employee> participants) {
        this.name = name;
        this.messages = new ArrayList<>();
        this.participants = participants;
    }

    public boolean checkIfDataEquals(Channel channel) {
        return name.equals(channel.getName()) &&
                compareParticipants(channel.getParticipants());
    }

    private boolean compareParticipants(List<Employee> participantList) {
        if (participantList.isEmpty())
            return true;
        for (Employee employee : participants) {
            if (participantList.stream().noneMatch(t -> t.checkIfDataEquals(employee)))
                return false;
        }
        return true;
    }
}

在channelDTO中设置messagesDTOs有问题,因为我得到:java.lang.StackOverflowError: null

    at com.herokuapp.erpmesbackend.erpmesbackend.chat.ChannelDTO.<init>(ChannelDTO.java:24) ~[classes/:na]
    at com.herokuapp.erpmesbackend.erpmesbackend.chat.MessageDTO.<init>(MessageDTO.java:23) ~[classes/:na]

对于课程:

message D to.Java

@Data
@AllArgsConstructor
public class MessageDTO {

    private long id;
    private String content;
    private EmployeeDTO authorDTO;
    private ChannelDTO channelDTO;
    private LocalDateTime creationTime;

    public MessageDTO(Message message) {
        this.id = message.getId();
        this.content = message.getContent();
        this.authorDTO = new EmployeeDTO(message.getAuthor());
        this.channelDTO = new ChannelDTO(message.getChannel()); // PROBLEMATIC LINE
        this.creationTime = message.getCreationTime();
    }

    public MessageDTO(String content, EmployeeDTO authorDTO, ChannelDTO channelDTO) {
        this.content = content;
        this.authorDTO = authorDTO;
        this.channelDTO = channelDTO;
    }

    public boolean checkIfDataEquals(MessageDTO messageDTO) {
        return content.equals(messageDTO.getContent()) &&
                authorDTO.checkIfDataEquals(messageDTO.getAuthorDTO()) &&
                channelDTO.checkIfDataEquals(messageDTO.getChannelDTO());
    }
}

channel D to.Java

@Data
@AllArgsConstructor
public class ChannelDTO {

    private long id;
    private String name;
    private List<MessageDTO> messageDTOs;
    private List<EmployeeDTO> participantDTOs;

    public ChannelDTO(Channel channel) {
        this.id = channel.getId();
        this.name = channel.getName();
        this.messageDTOs = new ArrayList<>();
        for (int i = 0; i < channel.getMessages().size(); i++)
            messageDTOs.add(new MessageDTO(channel.getMessages().get(i)));  // PROBLEMATIC LINE
        this.participantDTOs = new ArrayList<>();
        channel.getParticipants().forEach(participant -> this.participantDTOs.add(new EmployeeDTO(participant)));
    }

    public ChannelDTO(String name, List<EmployeeDTO> participantDTOs) {
        this.name = name;
        this.participantDTOs = participantDTOs;
    }

    public boolean checkIfDataEquals(ChannelDTO channelDTO) {
        return name.equals(channelDTO.getName()) &&
                compareParticipantDTOs(channelDTO.getParticipantDTOs());
    }

    private boolean compareMessageDTOs(List<MessageDTO> messageDTOList) {
        if (messageDTOList.isEmpty())
            return true;
        for (MessageDTO messageDTO: messageDTOs) {
            if (messageDTOList.stream().noneMatch(t -> t.checkIfDataEquals(messageDTO)))
                return false;
        }
        return true;
    }

    private boolean compareParticipantDTOs(List<EmployeeDTO> participantDTOList) {
        if (participantDTOList.isEmpty())
            return true;
        for (EmployeeDTO participantDTO : participantDTOs) {
            if (participantDTOList.stream().noneMatch(t -> t.checkIfDataEquals(participantDTO)))
                return false;
        }
        return true;
    }
}

message controller.Java:

@RestController
@CrossOrigin(origins = "*")
public class MessageController {

    private final MessageRepository messageRepository;
    private final ChannelRepository channelRepository;
    private final EmployeeRepository employeeRepository;

    @Autowired
    public MessageController(MessageRepository messageRepository, ChannelRepository channelRepository,
                             EmployeeRepository employeeRepository) {
        this.messageRepository = messageRepository;
        this.channelRepository = channelRepository;
        this.employeeRepository = employeeRepository;
    }

    @PostMapping("/channels/{id}/messages")
    @ResponseStatus(HttpStatus.CREATED)
    public MessageDTO addOneMessage(@PathVariable Long id, @RequestBody MessageRequest messageRequest) {
        checkIfChannelExists(1L);

        String content = messageRequest.getContent();
        Channel channel = channelRepository.findById(id).get();

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = ((UserDetails) principal).getUsername();
        Employee author = employeeRepository.findByEmail(username).get();

        Message message = new Message(content, author, channel);
        messageRepository.save(message);
        return new MessageDTO(message);
    }

    private void checkIfChannelExists(Long id) {
        if (!channelRepository.findById(id).isPresent())
            throw new NotFoundException("Such channel doesn't exist!");
    }
}

channel controller.Java

@RestController
@CrossOrigin(origins = "*")
public class ChannelController {

    private final ChannelRepository channelRepository;
    private final EmployeeRepository employeeRepository;

    @Autowired
    public ChannelController(ChannelRepository channelRepository, EmployeeRepository employeeRepository) {
        this.channelRepository = channelRepository;
        this.employeeRepository = employeeRepository;
    }

    @GetMapping("/channels")
    @ResponseStatus(HttpStatus.OK)
    public List<ChannelDTO> getAllChannels() {
        List<Channel> channels = channelRepository.findAll();
        List<ChannelDTO> channelDTOs = new ArrayList<>();
        channels.forEach(channel -> channelDTOs.add(new ChannelDTO(channel)));
        return channelDTOs;
    }

    @GetMapping("/channels/{id}")
    @ResponseStatus(HttpStatus.OK)
    public ChannelDTO getOneChannel(@PathVariable("id") Long id) {
        checkIfChannelExists(id);
        return new ChannelDTO(channelRepository.findById(id).get());
    }

    @GetMapping("/employees/{id}/channels")
    @ResponseStatus(HttpStatus.OK)
    public List<ChannelDTO> getChannelsByParticipant(@PathVariable("id") Long id) {
        checkIfParticipantExists(id);

        if (!channelRepository.findByParticipantsId(id).isPresent())
            return new ArrayList<>();

        List<Channel> channels = channelRepository.findByParticipantsId(id).get();
        List<ChannelDTO> channelDTOs = new ArrayList<>();
        channels.forEach(channel -> channelDTOs.add(new ChannelDTO(channel)));

        return channelDTOs;
    }

    @PostMapping("/channels")
    @ResponseStatus(HttpStatus.CREATED)
    public ChannelDTO addOneChannel(@RequestBody ChannelRequest channelRequest) {
        String name = channelRequest.getName();

        checkIfParticipantListIsEmpty(channelRequest.getParticipantIds());
        List<Employee> participants = new ArrayList<>();
        channelRequest.getParticipantIds().forEach(this::checkIfParticipantExists);
        channelRequest.getParticipantIds().forEach(id -> participants.add(employeeRepository.findById(id).get()));

        Channel channel = new Channel(name, participants);
        channelRepository.save(channel);
        return new ChannelDTO(channel);
    }

    @PutMapping("/channels/{id}")
    public HttpStatus updateChannel(@PathVariable("id") Long id, @RequestBody ChannelRequest channelRequest) {
        checkIfChannelExists(id);
        Channel channel = channelRepository.findById(id).get();

        channel.setName(channelRequest.getName());

        checkIfParticipantListIsEmpty(channelRequest.getParticipantIds());
        List<Employee> participants = new ArrayList<>();
        if (channelRequest.getParticipantIds() != null) {
            channelRequest.getParticipantIds().forEach(this::checkIfParticipantExists);
            channelRequest.getParticipantIds().forEach(index -> participants.add(employeeRepository.findById(index).get()));
        }
        channel.setParticipants(participants);

        channelRepository.save(channel);
        return HttpStatus.NO_CONTENT;
    }

    @DeleteMapping("/channels/{id}")
    public HttpStatus removeChannel(@PathVariable("id") Long id) {
        checkIfChannelExists(id);
        channelRepository.delete(channelRepository.findById(id).get());
        return HttpStatus.OK;
    }

    private void checkIfChannelExists(Long id) {
        if (!channelRepository.findById(id).isPresent())
            throw new NotFoundException("Such channel doesn't exist!");
    }

    private void checkIfParticipantExists(Long id) {
        if (!employeeRepository.findById(id).isPresent())
            throw new NotFoundException("At least one of the participant doesn't exist!");
    }

    private void checkIfParticipantListIsEmpty(List<Long> participantIds) {
        if (participantIds.isEmpty())
            throw new InvalidRequestException("List of participants can't be empty!");
    }
}

有人可以帮忙吗?

hibernate spring-data-jpa spring-data one-to-many hibernate-mapping
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.