我有一个实体菜单,有一个儿童关系餐厅。我会检查是否有餐厅有菜单,菜单不能删除,所以我做了这个junit测试:
Restaurant resto = new Restaurant(menu);
restaurantService.save(resto);
menuService.delete (menu);
menu = menuService.findByMenuId(menuName);
assertNotNull (menu);
但是我当然不能测试这个用户案例,因为我有这个例外:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
public class Menu {
...
@OneToMany(mappedBy = "menu",
cascade = CascadeType.ALL,
orphanRemoval = true, fetch = FetchType.LAZY)
@JsonIgnore
private Set<Restaurants> restaurant = new HashSet<>();
...
}
和
public class Restaurant {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "menu_id")
@JsonIgnore
private Menu topMenu;
..
}