根据应用程序的逻辑,我有一个控制器来删除回收站。通过链接,我们获得会话ID,通过它我们在数据库中搜索用户并删除与之关联的所有发票。
@RequestMapping(path="/basket/del/{sessId}", method=RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public BasketListReply delAllProducts(@PathVariable String sessId){
BasketListReply rep = new BasketListReply();
try {
//check if session exist
List<Long> delList = userService.findInvoiceBySessionId(sessId);
//delList contain list of invoices id to delete
if(delList != null) {
for(int i=0;i<delList.size();i++){
invoiceService.delInvoice(delList.get(i));
}
}
}catch(Exception e){
rep.retcode = -1;
rep.error_message = e.getMessage();
}
return rep;
}
使用方法findInvoiceBySessionId(),我可以从collection()中获取要删除的发票清单。
public List findInvoiceBySessionId(String sessId) throws Exception{
List list = new ArrayList();
List<Appuser> users = findUserBySessionId(sessId);
if(!users.isEmpty()) {
//if exist, then extract invoices for this user
Appuser u = users.get(0);
List<Invoice> invoices = (List<Invoice>)u.getInvoiceCollection();
for(Invoice inv:invoices) {
list.add(inv.getOrderId());
}
}
return list;
}
方法delInvoice()我用来从存储库中删除发票。
public void delInvoice(Long orderId){
if(invoiceRepository.findOne(orderId) == null) {
return;
}
invoiceRepository.delete(orderId);
}
文件Appuser.java(包含发票集合的字段):
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
private Collection<Invoice> invoiceCollection;
我是否正确理解我不能在一个控制器中通过JPA从数据库中提取数据并删除相同的数据...因为删除应该在单独的事务中进行。 或者我做错了什么? 当我通过将请求重定向到控制器以删除数据来删除数据时,一切正常。