如何解决如何将putMapping中的存储库与SpringBoot中的getMapping中的存储库区分开?
我有一个仿真对象,其中有一个Set作为其字段之一。模拟对象还具有带有房间的Home。 Room还具有Set作为其字段之一。我正在尝试从其中一个房间中删除一个用户,并将其保存到Simulation对象。我可以成功地将用户保存在Simulation对象中,但是不能将其从Room中删除。
SimulationUsers 很有趣。
模拟对象:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class Simulation {
@Id
public String name;
private double temperature;
private String date;
private String time;
@OneToOne
private Home home;
@OneToMany
private Set<SimulationUser> simulationUsers;
}
带有家庭和房间对象:
@Data
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name = "home")
public class Home {
@Id
@GeneratedValue
private Long id;
@NonNull
private String name;
...
@OneToMany(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
private Set<Room> rooms;
@OneToMany(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
private Set<Sensor> sensors;
}
和
public class Room {
@Id
@GeneratedValue
private Long id;
...
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
private Set<SimulationUser> simulationUsers;
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private Set<Sensor> sensors;
....
}
我的存储库都是以下格式:
@Repository
public interface SimulationRepository extends JpaRepository<Simulation,String> {
}
我的控制器是
@Slf4j
@RestController
@RequestMapping("/api")
public class SimuationController {
public HomeRepository homeRepository;
public SimulationRepository simulationRepository;
public SimuationController(HomeRepository homeRepository,SimulationRepository simulationRepository) {
this.homeRepository = homeRepository;
this.simulationRepository = simulationRepository;
}
....
@PutMapping("/simulation/user-rooms")
public ResponseEntity<Simulation> updateSimulation(@Valid @RequestBody Simulation simulation){
Simulation currentSimulation = simulationRepository.findById("1").get();
currentSimulation.setHome(simulation.getHome());
currentSimulation.setSimulationUsers(simulation.getSimulationUsers());
Simulation createdSimulation = simulationRepository.save(simulation);
log.info("The saving simulation is {}",createdSimulation);
log.info("After saving the simulation is {}",simulationRepository.findAll());
return ResponseEntity.ok().body(createdSimulation);
}
@GetMapping("/simulation")
public ResponseEntity<Simulation> getSimulation() {
Simulation simulation = simulationRepository.findById("1").get();
log.info("After getting the simulation is {}",simulationRepository.findAll());
return ResponseEntity.ok().body(simulation);
}
问题在于,在我的@PutMapping updateSimulation方法中,要保存的对象和保存后的存储库状态与我在@GetMapping方法中得到的不一样。
输出(有趣的部分用星号括起来:
在Get方法内在之前:
log: After getting the simulation is [Simulation(... home=Home(... rooms=[... lights=[],**simulationUsers=[SimulationUser(id=3,name=User1,privilege=Parent),SimulationUser(id=4,name=User2,privilege=Child)]**,sensors=[])...],sensors=[]),simulationUsers=[])]
在Put方法内部:
log: Before saving the simulation (repository) is [Simulation(... home=Home(... rooms=[... lights=[],simulationUsers=[SimulationUser(id=4,privilege=Child)])]
log: The saving simulation is .....................Simulation(... home=Home(... rooms=[... lights=[],privilege=Parent)]**,privilege=Child)])]
log: After saving the simulation (repository) is. [Simulation(... home=Home(... rooms=[... lights=[],privilege=Child)])]
在put方法之后,在GetMethod中:
log: After getting the simulation is [Simulation(... home=Home(... rooms=[... lights=[],privilege=Child))]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。