我有一个基本的SpringBoot应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎并将其打包为可执行JAR文件。
我创建了这个Repository类:
@Repository
public interface MenuRepository extends CrudRepository<Menu, Long> {
..
}
和这个服务等级
@Service
@Transactional(readOnly = true)
public class MenuService {
@Autowired
protected MenuRepository menuRepository;
@Transactional
public void delete (Menu menu) {
menuRepository.delete (menu);
}
..
}
这个Junit测试:
@ContextConfiguration(classes={TestSystemConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MenuGestApplication.class)
public class MenuServiceTests {
...
@Test
public void testDelete () {
Menu menu = new menu();
menu.setmenuId("bacalla-amb-tomaquet");
menuService.save(menu);
MenuPrice menuPrice = new menuPrice(menu);
menuPrice.setPrice((float)20.0);
menuPriceService.save(menuPrice);
MenuPriceSummary menuPriceSummary = new menuPriceSummary(menu);
menuPriceSummary.setFortnightlyAvgPrice((float)20.0);
menuPriceSummaryService.save(menuPriceSummary);
menu = menuService.findBymenuId("bacalla-amb-tomaquet");
assertNotNull (menu);
menuService.delete (menu);
menu = menuService.findBymenuId("bacalla-amb-tomaquet");
assertNull (menu);
}
}
但是Junit失败了,因为没有删除对象并且没有抛出异常!
我建议这样做。
@OneToMany(mappedBy="menu", cascade = CascadeType.ALL, orphanRemoval = true, fetch=FetchType.LAZY)
private List<MenuPrice> price;
即使我在运行测试时在控制台中看到了这一点:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`elcormenu`.`t_menu_price`, CONSTRAINT `FK19d0sljpshu4g8wfhrkqj7j7w` FOREIGN KEY (`menu_id`) REFERENCES `t_menu` (`id`))
和Menu类:
@Entity
@Table(name="t_menu")
public class Menu implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty("id")
private Long id;
@JsonProperty("MenuId")
private String MenuId;
@OneToMany(mappedBy = "Menu", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JsonIgnore
private Set<MenuPrice> MenuPrice = new HashSet<>();
@OneToOne(mappedBy = "Menu", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private MenuPriceSummary summary;
...
}
最佳答案
您将同时使用@OneToMany
和@ManyToOne
定义Menu和MenuPrice之间的双向关系。当您具有生物定向关系时,您需要设置双方。在测试中,您要在MenuPrice中设置Menu,但不将MenuPrice添加到Menu的MenuPrice Set中。创建menuPrice之后,请包含以下语句menu.MenuPrice.add(menuPrice);
。由于您已经指定了save()
,因此您也不需要多个Cascade.All
。请尝试以下方法:
public void testDelete () {
Menu menu = new menu();
menu.setmenuId("bacalla-amb-tomaquet");
MenuPrice menuPrice = new menuPrice(menu);
menuPrice.setPrice((float)20.0);
// Not needed
// menuPriceService.save(menuPrice);
// Add menuPrice to menu's menuPrice set
menu.menuPrice.add(menuPrice);
MenuPriceSummary menuPriceSummary = new menuPriceSummary(menu);
menuPriceSummary.setFortnightlyAvgPrice((float)20.0);
// Set menuPriceSummary in menu
menu.summary = menuPriceSummary;
// Not needed
//menuPriceSummaryService.save(menuPriceSummary);
// Saving menu will save it children too
menuService.save(menu);
menu = menuService.findBymenuId("bacalla-amb-tomaquet");
assertNotNull (menu);
menuService.delete (menu);
menu = menuService.findBymenuId("bacalla-amb-tomaquet");
assertNull (menu);
}
在某些情况下,您不需要双向关系,可以删除
@OneToMany
,但这取决于您的业务逻辑需要如何导航到子级或通过JPA查询它。关于java - CrudRepository不会删除具有关系的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50818106/