Spring Boot从两个实体JHipster中删除多行

时间:2017-07-02 15:45:19

标签: spring-boot spring-data-jpa jhipster

我在项目中使用Spring Boot作为后端。在函数deleteDoctor中,我想从表doctor中删除id的医生,同时我想删除user表中具有登录名的关联user doctor

但是,流程不起作用,代码参考如下:

@DeleteMapping("/doctor/{id}")
@Timed
public ResponseEntity<Void> deleteDoctor(@PathVariable String id,
                                         @RequestBody Doctor doctor) {
  log.debug("REST request to delete Doctor : {}", id);
  doctorRepository.delete(id);

  userRepository.findOneByLogin(doctor.getLogin()).
    ifPresent(user -> {
      userRepository.delete(user);
      log.debug("Deleted User: {}", user);
    });

  return ResponseEntity.ok()
    .headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
}

1 个答案:

答案 0 :(得分:0)

使用deleteDoctor()或更高版本注释@Transactional方法:将该代码移至使用@Transactional的服务方法