PO类(订单):
@Entity
@Table(name = "t_order")
public class Order {
@Id
@GenericGenerator(name = "myuuid", strategy = "uuid")
@GeneratedValue(generator = "myuuid")
private String orderNum;
private String receiverInfo;
@Column(precision = 23, scale = 2) // 小数点后保留两位,整体精度是23位
private BigDecimal price;
@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name = "customer_id")
private Customer customer;
...get,set方法
}
PO类(客户):
@Entity
@Table(name = "t_customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String cusName;
private String cusPhone;
// 上传的照片地址
private String cusImgSrc;
@OneToMany(mappedBy = "customer", targetEntity = Order.class)
@Cascade(CascadeType.DELETE)//级联删除
private Set<Order> orders = new HashSet<Order>();
...get,set方法
}
(前端页面删除客户方法)
<script type="text/javascript">
function delCustomer(customerId){
location.href = "${pageContext.request.contextPath}/customer/delCustomer?id="+customerId;
}
</script>
视图层
@Controller
@Scope("prototype")
@Namespace("/customer")
@ParentPackage("struts-default")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer();
@Autowired
private CustomerService customerService;
@Action(value = "delCustomer", results = {
@Result(name = "success", location = "findAllCustomer", type = "redirectAction") })
public String delCustomer() {
System.err.println(customer);
customerSerSystem.err.println(customer);vice.delCustomer(customer);
return "success";
}
@Action(value = "findAllCustomer", results = { @Result(name = "success", location = "/customerList.jsp") })
public String findAllCustomer() {
List<Customer> cs = customerService.findAllCustomer();
ActionContext.getContext().getValueStack().set("cs", cs);
return "success";
}
@Override
public Customer getModel() {
return customer;
}
}
(业务层中的删除客户的方法)
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public void delCustomer(Customer customer) {
//查询要删除的客户所有信息
customer =customerDao.findCustomerById(customer.getId());
customerDao.delCustomer(customer);
}
}
(dao层方法)
@Repository
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Autowired
public void setSupportSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public void delCustomer(Customer customer) {
this.getHibernateTemplate().delete(customer);
}
@Override
public Customer findCustomerById(Integer id) {
return (Customer)this.getHibernateTemplate().get(Customer.class, id);
}
}
在数据库手动插入几条数据:
如以上代码和数据,张三下面没有订单,李四下面有订单asd123,
在前端页面我们点击删除客户,根据前端代码,
此时页面会将该客户的id发送给后台,然后根据客户id进行删除.如果我们在业务层将查询用户所有信息的方法删除,直接调用dao层删除客户方法
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public void delCustomer(Customer customer) {
//查询要删除的客户所有信息
//customer =customerDao.findCustomerById(customer.getId());
customerDao.delCustomer(customer);
}
}
会出现两种结果:
1.如果该用户下面没有订单,那么可以删除成功,在视图层我们为了方便查看结果插入了一行代码(视图层下划线处),得到了用户的信息,只有用户id
2.如果该用户下面有订单,那么删除失败,
控制台报的错误很多,第一条如下:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`sshtest`.`t_order`, CONSTRAINT `FKesy3n2gc3fa0s3trrk3tvyv9a` FOREIGN KEY (`customer_id`) REFERENCES `t_customer` (`id`))
解决方法:在级联删除时,如果该用户下面有订单,在删除时一定要先将该用户的所有数据全部查出来封装好,再进行删除,才可以成功完成级联删除( 查看源码后,hibernate底层进行了一些判断,有兴趣的可以自己扒一扒源码)
完整的业务层级联删除方法:
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public void delCustomer(Customer customer) {
//查询要删除的客户所有信息(一定要将所有数据查出来封装好,再进行级联删除)
customer =customerDao.findCustomerById(customer.getId());
customerDao.delCustomer(customer);
}
}