Spring中@ManyToOne与@JoinColumn无法加载OsDto关联数据问题
问题:Device实体中OsDto关联字段无法加载,返回前端为null
相关代码
Device实体类
@Entity @Getter @Setter public class Device { @Id @org.springframework.data.annotation.Id @UuidGenerator private UUID id; /** * The hardware associated with the device. * * @see GenericHardwareData for more info */ @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "hardware") private GenericHardwareData hardware; /** * The storage capacity of the device. */ private int storage; /** * The operating system of the device. */ @ManyToOne @JoinColumn(name = "operation_system") private OsDto operationSystem; private String name; }
OsDto实体类
@Entity @Getter @Setter public class OsDto { @Id @org.springframework.data.annotation.Id @UuidGenerator private UUID id; @Column(unique = true) private String name; }
HardwareController控制器代码
@Slf4j @RestController @RequestMapping("/hardware") public class HardwareController { private final HardwareRepository hardwareRepository; private final HardwareService hardwareService; @Autowired public HardwareController(HardwareRepository hardwareRepository, HardwareService hardwareService) { this.hardwareRepository = hardwareRepository; this.hardwareService = hardwareService; } @GetMapping @Secured({"ADMIN", "VIEW"}) public ResponseEntity<?> getHardware() { return ResponseEntity.ok(this.hardwareRepository.findAll()); } @GetMapping("/find") @Secured({"ADMIN", "VIEW"}) public ResponseEntity<?> getHardwareByQrCode(@RequestParam("qrcode") String qrCode) { try { int qr = Integer.parseInt(qrCode); return ResponseEntity.of(this.hardwareRepository.findByQrCode(qr)); } catch (NumberFormatException e) { return ResponseEntity.badRequest().body("{\"error\": \"qr code can't be parsed to integer\"}"); } } @GetMapping("/device") @Secured({"ADMIN", "VIEW"}) public ResponseEntity<?> getComputerByQrCode(@RequestParam("qrcode") String qrCode, @RequestParam("type") String rawHardwareType) { try { HardwareType hardwareType = HardwareType.valueOf(rawHardwareType); switch (hardwareType) { case COMPUTER -> { return this.hardwareService.getComputerFromQrCode(qrCode); } case DOCKING_STATION -> { return this.hardwareService.getDockingStationByQrCode(qrCode); } case MOBILE -> { return this.hardwareService.getMobileFromQrCode(qrCode); } case MONITOR -> { return this.hardwareService.getMonitorByQrCode(qrCode); } case PRINTER -> { return this.hardwareService.getPrinterByQrCode(qrCode); } } } catch (IllegalArgumentException e) { return ResponseEntity.badRequest().body("{\"error\": \"hardware type couldn't be serialized\"}"); } return ResponseEntity.notFound().build(); } @GetMapping("/find/{type}") @Secured({"ADMIN", "VIEW"}) public ResponseEntity<?> getHardwareByType(@PathVariable String type) { try { return ResponseEntity.ok(this.hardwareRepository.findAllByHardwareType(HardwareType.valueOf(type.toUpperCase()))); } catch (IllegalArgumentException e) { return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); } } }
当前问题与排查情况
- 前端Angular应用的Chrome日志显示,接口返回的Device对象中
operationSystem字段为null - 已确认数据库中:
- device表的
operation_system字段存储了正确的关联UUID - os_dto表中存在对应UUID的OsDto数据
- device表的
- 尝试给Device类中
operationSystem字段的@ManyToOne注解添加FetchType.EAGER属性,问题未解决,该字段仍为null
内容的提问来源于stack exchange,提问作者DasShorty
相关产品推荐
相关产品推荐

