当我试图从 REST 控制器中删除下面的 Customer 对象时,得到“删除分离的实例”异常。
日志:
org.springframework.dao.InvalidDataAccessApiUsageException: Removing a detached instance com.test.model.Customer#1750; nested exception is java.lang.IllegalArgumentException: Removing a detached instance com.test.model.Customer#1750
领域:
@Entity
public class Customer{
@Id
private Long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;
// other stuff with getters/setters
}
休息控制器:
@Controller
@RequestMapping("/shop/services/customers")
public class CustomerRESTController {
/**
* Deletes a customer
*/
@RequestMapping( value="/{id}", method=RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCustomer(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) throws Exception {
Customer customer = customerService.getById(id);
if(customer != null){
customerService.delete(customer);
}else{
response.sendError(503, "No Customer found for ID : " + id);
}
}
// other stuff
}
我正在从数据库中获取客户对象,但仍然在休眠抱怨。有什么建议吗??