你能帮我解决JPA Repository吗?在更新请求和使用POST方法期间 - 我只需要保存我发送到POST主体的字段。但是,如果我没有将它写入正文,则JPA Repository set为null(DB MySQL,默认为null)。请看图片。
如果我没有发送bode机场代码,则在更新后无效。但它是" UAAA"。我该如何管理它?谢谢你的建议!
模特课切:
@Entity
@SelectBeforeUpdate
@DynamicUpdate
@Table(name = "airports")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Airports {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String airport;
private String code;
控制器类切割:
@RestController
public class AirportsController {
private AirportsRepository repository;
public AirportsController(AirportsRepository repository) {
this.repository = repository;
}
@PostMapping(value = "airports/update")
public ResponseEntity updateAirportById(@RequestBody Airports airports) {
Airports foundedById = repository.findOne(airports.getId());
foundedById.setAirport(airports.getAirport());
foundedById.setCode(airports.getCode());
repository.save(foundedById);
return new ResponseEntity(foundedById, HttpStatus.OK);
}
答案 0 :(得分:4)
这是因为您在控制器updateAirportById方法中设置了值,即使它们为null。 如果您没有在正文中传递“code”和“airport”的值,则方法getAirports将返回null并将其设置为founderById然后更新它。如果条件应该解决这个问题很简单
if(airports.getAirport() != null)
foundedById.setAirport(airports.getAirport());
“代码”也是如此