@Autowired
LessonService lsnService;
@PutMapping(path = "/{id}")
public ResponseEntity<Object> updateLesson(@PathVariable("id") Long id, @Valid @RequestBody LessonDto dto) {
try {
lsnService.findById(id);
dto.setId(id);
lsnService.save(dto);
return ResponseEntity.ok(dto);
}
catch (Exception e) {
ApiErrorMessage errorMessage = new ApiErrorMessage();
errorMessage.setStatusCode(400L);
errorMessage.setMessage(e.getMessage());
errorMessage.setDescription("The server cannot or will not process the request due to an apparent client error");
return ResponseEntity.badRequest().body(errorMessage);
}
}
Вот моя проблема. Когда я удаляю lsnService.findById(id);
, обновление работает.
Если я не добавлю этот код, если пользователь обновляет с несуществующим идентификатором, он сохранит другие данные. Другая проблема - когда я удаляю dto.setId(id);
, оба метода из lsnService; findById(id);
и save(dto);
работают! Но, как вы можете видеть, репо должно обновить сущность, но не будет !!!
Итак, я попытался поместить @Transactional
в сохранение. И я даже пытаюсь поставить задержку Thread.sleep(5000);
5 секунд между этими двумя службами. Вот так
lsnService.findById(id);
Thread.sleep(5000);
dto.setId(id);
lsnService.save(dto);
Но это тоже не работает!
@Autowired
private LessonJpaRepository repo;
@Override
public LessonDto findById(Long id) {
// TODO Auto-generated method stub
Lesson lesson = repo.getOne(id);
LessonDto dto = new LessonDto(lesson);
return dto;
}
@Override
public void save(LessonDto dto) {
// TODO Auto-generated method stub
repo.save(dto.getEntity());
System.out.println(dto.getId()+dto.getTitle()+dto.getStructure()+dto.getExplanation());
}
А потом я проверяю вывод этого dto. Это все там! РЕПО не сохраняет его! Это так странно для меня. Есть идеи?
public class LessonDto {
private Long id;
@NotNull(message = "Title must not be null")
@NotBlank(message = "Title must not be blank")
@ValidLessonTitle(message = "Title must begin with uppercase character")
private String title;
@NotNull(message = "Structure must not be null")
@NotBlank(message = "Structure must not be blank")
private String structure;
@NotNull(message = "Explanation must not be null")
@NotBlank(message = "Explanation must not be blank")
private String explanation;
public LessonDto() {
}
public LessonDto(Lesson lesson) {
this.id=lesson.getId();
this.title=lesson.getTitle();
this.structure=lesson.getStructure();
this.explanation=lesson.getExplanation();
}
@java.beans.Transient
public Lesson getEntity() {
Lesson lesson = new Lesson();
lesson.setId(this.id);
lesson.setTitle(this.title);
lesson.setStructure(this.structure);
lesson.setExplanation(this.explanation);
return lesson;
}
//getters and setters
}
Это сущность
@Entity
public class Lesson implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2239534946567783017L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "structure")
private String structure;
@Column(name = "explanation")
private String explanation;
//getters and setters
}