Unable to get complete subarray information for entities in Spring Boot application

Unable to Retrieve Complete Group Information for Messages in a Spring Boot Application.
Messages and groups represent data entities in a Spring Boot application. Messages are textual messages that have various properties, such as text content, deadlines, and a list of associated groups. Groups, on the other hand, are collections of messages, and each group can be associated with multiple messages. The issue relates to not being able to retrieve the full set of associated groups for each message in the application.
When I manually populate the database, everything is displayed correctly. The problem is most likely in the add method, but I can’t figure out what the issue is.

@GetMapping("/")
    public String greeting(Model model) {
        Iterable<Message> notifications = messageService.findAll();
        model.addAttribute("notifications", notifications);
        model.addAttribute("groupList", groupService.findAll());
        return "index";
    }

@PostMapping
    public RedirectView add(@RequestParam String text, @RequestParam LocalDateTime deadline,
                      @RequestParam Set<Long> groups){

        System.out.println(groupService.findExistById(groups));
        Message notification = new Message(text, deadline, groupService.findExistById(groups));
        messageService.save(notification);

        return new RedirectView("/");
    }

findAll provides:

Message(id=2, text=ddsa, deadline=2023-11-05T01:57, isRelevant=true, sendingDate=2023-11-05T01:57:45.766761, numOfNotif=3, groups=[Group(id=2, chatId=13, groupName=CSE-2305M, notifications=[])]), 
Message(id=3, text=dwefsef, deadline=2023-11-05T01:57, isRelevant=true, sendingDate=2023-11-05T01:57:57.040415, numOfNotif=3, groups=[]), 
Message(id=4, text=rwerew, deadline=2023-11-05T01:58, isRelevant=true, sendingDate=2023-11-05T01:58:09.515409, numOfNotif=3, groups=[Group(id=3, chatId=379210, groupName=CSE-2304M, notifications=[])]), 
Message(id=5, text=rewrwrq, deadline=2023-11-05T01:58, isRelevant=true, sendingDate=2023-11-05T01:58:20.857396, numOfNotif=3, groups=[])]

Messages Entity

 
@Entity
@Data
@Table(name = "Messages")
public class Message {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "message_text", columnDefinition="TEXT")
    private String text;

    @Column(name = "deadline")
    private LocalDateTime deadline;

    @Column(name = "is_relevant")
    private Boolean isRelevant = true;

    @Column(name = "sending_date")
    private LocalDateTime sendingDate = LocalDateTime.now();

    @Column(name = "num_of_notif")
    private Integer numOfNotif = 3;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "Notifications_Groups",
            joinColumns = @JoinColumn(name = "notif_id"),
            inverseJoinColumns = @JoinColumn(name = "group_id"))
    private Set<Group> groups = new HashSet<>();

    public Message() {
    }

    public Message(String text, LocalDateTime deadline, Set<Group> groups) {
        this.text = text;
        this.deadline = deadline;
        this.groups = groups;
    }

}

Groups entity

@Entity
@Data
@Table(name = "Student_Groups")
public class Group {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "chat_id")
    private Long chatId;

    @Column(name = "group_name")
    private String groupName;

    @ManyToMany(mappedBy = "groups")
    private Set<Message> notifications = new HashSet<>();

    public Group() {
    }

}

MessageServiceImplementation

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageRepo messageRepo;

    @Override
    public List<Message> findAll() {
        return messageRepo.findAll();
    }

    @Override
    public void save(Message message) {
        messageRepo.save(message);
    }
}

And the screen below provides data from database

I’ve checked the SQL query generated when calling the findAll() method in my MessageService. I used SQL query logging to ensure that the query is correct and returns the expected data.

Leave a Comment