Spring Boot Hibernate ManyToOne关联joinColumn外键值为null问题
问题根因
你当前的实体关联配置本身没有问题,出现外键为null的原因是上传流程中从未对FileUpload实体的user属性进行赋值,JPA不会自动完成关联关系的绑定,因此user_id字段会存储空值。
修复步骤
1. 修改FileUploadService的store方法
新增User类型入参,创建FileUpload实例后手动绑定关联用户:
@Service public class FileUploadService { @Autowired private FileUploadRepository fileUploadRepository; // 新增User参数 public FileUpload store(MultipartFile file, User user) throws IOException { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); FileUpload fileDB = new FileUpload(fileName, file.getContentType(), file.getBytes()); // 手动绑定用户关联 fileDB.setUser(user); return fileUploadRepository.save(fileDB); } // 其余原有方法保持不变 }
2. Controller层获取用户并传入service
根据你的业务场景,选择以下任意一种方式获取当前操作用户:
方式1:通过Spring Security安全上下文获取已登录用户(推荐)
需要先在Controller中注入你的UserRepository,然后从安全上下文读取登录用户信息:
@Controller @CrossOrigin() public class FileUploadController { @Autowired private FileUploadService fileUploadService; // 注入UserRepository @Autowired private UserRepository userRepository; @PostMapping("/upload") public ResponseEntity<?> uploadFile(@RequestParam("file")MultipartFile file) { String message = ""; try { // 从安全上下文获取当前登录用户身份 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String loginAccount = authentication.getName(); // 查询用户实体 User loginUser = userRepository.findByEmail(loginAccount) .orElseThrow(() -> new RuntimeException("登录用户不存在")); // 调用service时传入用户实体 fileUploadService.store(file, loginUser); message = "Uploaded the file successfully: " + file.getOriginalFilename(); return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse(true, message)); } catch (Exception e) { message = "Could not upload the file: " + file.getOriginalFilename() + "!"; return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ApiResponse(true, message)); } } // 其余原有方法保持不变 }
方式2:前端上传时额外传递userId参数
如果还未接入Spring Security,可以暂时让前端在上传请求中额外携带userId参数,后端查询用户后绑定关联:
@PostMapping("/upload") public ResponseEntity<?> uploadFile(@RequestParam("file")MultipartFile file, @RequestParam("userId") Long userId) { String message = ""; try { User loginUser = userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("用户不存在")); fileUploadService.store(file, loginUser); message = "Uploaded the file successfully: " + file.getOriginalFilename(); return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse(true, message)); } catch (Exception e) { message = "Could not upload the file: " + file.getOriginalFilename() + "!"; return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ApiResponse(true, message)); } }
注意:该方式需要额外做权限校验,避免用户恶意修改其他用户的头像信息。
内容的提问来源于stack exchange,提问作者ServletException
相关产品推荐
相关产品推荐

