将多个文件发布到数据库 - ManyToOne 关系的空值

Posting multiple files to Database - Null value for ManyToOne relationship

我有两个具有 onetomany 和 manytoone 关系的实体,如下所示:

@Entity
@Table(name="file_model")
public class FileModel {
....
@JoinColumn(name = "userid", referencedColumnName = "id")
@ManyToOne(optional = false)
private AppUser appUser;
....
}


@Entity
@Table(name="app_user")
public class FileModel {
....
@OneToMany(cascade = CascadeType.ALL, mappedBy = "appUser")
private Set<FileModel> appUserFiles;
....
}

我正在尝试 post 使用 SpringBoot Data/JPA 多个文件到数据库。我的控制器中有以下内容。同时,我正在尝试为将要保存的每个文件设置用户标识。

@RequestMapping(value = { "/addDocument/{id}" }, method = RequestMethod.POST)
    public String uploadDocument(@RequestParam("files") MultipartFile[] files, @PathVariable Long id, ModelMap model)
            throws IOException {
        List<String> fileNames = new ArrayList<String>();
        AppUser appUser = appUserRepository.findById(id).get();
        System.out.println("User ID is :: " + appUser.getId());
        System.out.println("User EMail is :: " + appUser.getUseremail());
        try {
            List<FileModel> storedFile = new ArrayList<FileModel>();
            for (MultipartFile file : files) {
                FileModel fileModel = new FileModel();
                fileModel.setAppUser(appUser);
                fileModel = new FileModel(file.getOriginalFilename(), file.getContentType(), file.getBytes());
                fileNames.add(file.getOriginalFilename());
                storedFile.add(fileModel);
            }
            fileRepository.saveAll(storedFile);
            model.addAttribute("message", "All files uploaded successfully!");
            model.addAttribute("files", fileNames);
        } catch (Exception e) {
            model.addAttribute("message", "ERROR. File Upload Failed !!");
            model.addAttribute("files", fileNames);
            e.printStackTrace();
        }

        return "redirect:/addDocument/" + id;
    }

这是我在控制台得到的:

User ID is :: 1
User EMail is :: user1@mydomain.com
2020-07-19 16:37:43.951  WARN 39179 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
2020-07-19 16:37:43.952 ERROR 39179 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column "USERID"; SQL statement:
insert into file_model (created_at, updated_at, userid, mimetype, name, pic, id) values (?, ?, ?, ?, ?, ?, ?) [23502-200]
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

现在,由于我的 AppUser 对象已成功创建(从 id 和 email-id 的 sysout 值可以看出)为什么我会收到此异常?我在这里缺少什么?

你确实写了下面的代码:

for (MultipartFile file : files) {
            FileModel fileModel = new FileModel();
            fileModel.setAppUser(appUser);
            fileModel = new FileModel(file.getOriginalFilename(), file.getContentType(), file.getBytes());
            fileNames.add(file.getOriginalFilename());
            storedFile.add(fileModel);
        }

但问题是,首先您将 fileModel 设置为 new FileModel(),两行之后,您再次这样做,因此 fileModel.setAppUser(appUser) 行毫无意义。 就这样吧,它应该可以工作:

final FileModel fileModel = new FileModel(file.getOriginalFilename(), file.getContentType(), file.getBytes());
fileModel.setAppUser(appUser);
fileNames.add(file.getOriginalFilename());
storedFile.add(fileModel);

也尽量使用“final”。特别是在这种情况下,如果您使用“final”,您就会意识到您正在覆盖您的对象。

此致