首页 > 解决方案 > When a user updates their profile picture on my Spring web app and updates their profile, objects in a list belonging to the user are duplicated

问题描述

I am developing a web app with Spring.

This web app is a music sharing platform. With it a user can register and make a profile and can write posts or upload tracks. I have a user model with various attributes such as List and a track model. A user has a List of tracks (List) and when they upload a track it is added to this list.

One feature I want to have is for a user to upload a profile picture and I have created methods that allow this. However, when a user uploads this picture every track in the users list gets duplicated multiple times. So for example, if a user has a list containing 3 tracks called A, B, C, when they upload a profile picture their list might contain A, A, A, B, B, B, C, C, C or some other number of duplications. Furthermore, every time that user adds a new unrelated post the list will continue to duplicate. This will always happen after the profile picture has been uploaded, but not before.

Below is my method in the controller. This calls the uploadPic method and passes it the file and current user.

@RequestMapping(value="profilepic", method = RequestMethod.POST)
    public String addProfilePic(@RequestPart(value = "file") MultipartFile file, Model model, Principal principal) {

        String name = principal.getName(); 
        User u = userService.findByUsername(name);

        this.amazonClient.uploadPic(file, u);

        return "redirect:/settings?bio_success";
    }

This method saves the picture to an AWS S3 bucket and then calls a service method.

public String uploadPic(MultipartFile multipartFile, User u) {
    String fileUrl = "";
    try {
        File file = convertMultiPartToFile(multipartFile);
        String fileName = generateFileName(multipartFile);
        System.out.println(fileName);
        fileUrl = endpointUrl + "/" + picBucketName + "/" + fileName;
        uploadPicTos3bucket(fileName, file);
        file.delete();       
        picService.save(u, fileName);

    } catch (Exception e) {
       e.printStackTrace();
    }
    return fileUrl;
}

The service method saves the picture in a MySQL table and calls a method to update the user. AmazonClient.java

@Override
public void save(User u, String fileName) {
    ProfilePic newPic = new ProfilePic();
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    newPic.setFileName(fileName);
    newPic.setPublishTime(timestamp);
    newPic.setUser(u);
    profilePicRepository.save(newPic);  
    u.setProfilePic(newPic);
    userService.update(u);
}

UserService.java

@Override
public void update(User user) {
    user.setPassword(user.getPassword());
    user.setRoles(new HashSet<>(roleRepository.findAll()));
    user.setFname(user.getFname());
    user.setLname(user.getLname());
    user.setFriends(user.getFriends());
    user.setIncomingFriendRequests(user.getIncomingFriendRequests());
    user.setOutgoingFriendRequests(user.getOutgoingFriendRequests());
    user.setProfilePic(user.getProfilePic());
    user.setComments(user.getComments());
    user.setPost(user.getPost());
    user.setTracks(user.getTracks());
    userRepository.save(user);
}

I have other features where a user can update their details similar to this but this problem does not occur and only happens with the uploading of a profile picture.

When I debug and step through every method the user is updated seems to update correctly, including the number of tracks.

I created the below little script to try find the problem and the output of this script is below. Maybe this will help get an idea of what is causing this.

        String name = principal.getName(); 
        User u = userService.findByUsername(name);


        this.amazonClient.uploadPic(file, u);
        for (Track t : u.getTracks()) {
            System.out.println(t.getTrackName());
        }

        User user2 = userService.findByUsername(name);
        for (Track t : user2.getTracks()) {
            System.out.println(t.getTrackName());
        }

Output: Liberate In the reds Pjanoo

Liberate Liberate Liberate Liberate Liberate Liberate Liberate Liberate Liberate In the reds In the reds In the reds In the reds In the reds In the reds In the reds In the reds In the reds Pjanoo Pjanoo Pjanoo Pjanoo Pjanoo Pjanoo Pjanoo Pjanoo Pjanoo

Should the output not be the same for both since I am finding the same User as usernames are unique? Does anyone have any ideas as to what might be causing this. Any help would be greatly appreciated!

标签: javamysqlspring

解决方案


我已将用户实体中的列表更改为设置,它似乎已经解决了问题,因为集合不能有重复项!


推荐阅读