I hava deleteNotice method using jpaRepository.
public Notice deleteNotice(int id) {
Notice notice = noticeRepository.findByIdAndVisible(id, true)
.orElseThrow(() -> new EntityNotFoundException(String.valueOf(id)));
notice.setVisible(false);
return noticeRepository.save(notice);
}
And there is an entity file.
@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Notice {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Size(min=4, max=10, message="Title should have at least 4 characters")
private String title;
@JsonIgnore
private boolean visible;
}
When I try this deleteNotice in controller,
@DeleteMapping("/notice/{noticeId}")
public ResponseEntity<Object> deleteNotice(@PathVariable int noticeId){
return ResponseEntity.ok(noticeService.deleteNotice(noticeId));
}
I got an error "Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction".
Could I get some advice for this problem?