在带有Spring的Spring应用程序中,我得到了一个表示数据库中实体的对象,其中包含多个级别的关联--父->和arrayLists。为了数据库完整性,我必须检查一些属性,如果结果正常,我需要映射关联。但是我不能在没有ConcurrentModificationException的情况下做到这一点,因为我是在迭代时修改对象。
这是我得到的对象:
{
"name": "menu test",
"description": "description test for menu",
"price": 15,
"menuCourses": [
{
"category": "Entrée",
"productsInCourse": [
{
"product": {
"id": 1
},
"extraCost": 0
}
]
},
{
"category": "Plat",
"productsInCourse": [
{
"product": {
"id": 4
},
"extraCost": 2
}
]
}
]
}
下面是与我有关的实体(我省略了getter、setter和constructors):
@Entity
public class Menu {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private Double price;
@OneToMany(
mappedBy = "menu",
cascade = CascadeType.ALL,
orphanRemoval = true)
private List<MenuCourse> menuCourses = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
private Restaurant restaurant;
public void addCourse(MenuCourse course) {
this.menuCourses.add(course);
course.setMenu(this);
}
@Entity
public class MenuCourse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(
mappedBy = "menuCourse",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<ProductInCourse> productsInCourse = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
private Category category;
@ManyToOne(fetch = FetchType.LAZY)
private Menu menu;
public void addProductInMenu(ProductInCourse productInCourse) {
this.productsInCourse.add(productInCourse);
productInCourse.setMenuCourse(this);
}
@Entity
public class ProductInCourse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Double extraCost;
@ManyToOne()
private Product product;
@ManyToOne(fetch = FetchType.LAZY)
private MenuCourse menuCourse;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String imageUrl;
@OneToMany(
mappedBy = "product",
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY
)
private List<ProductInCourse> productsInMenu = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
private Category category;
@ManyToOne(fetch = FetchType.LAZY)
private Restaurant restaurant;
我的业务层在持久化之前检查对象,
public Menu saveMenu(Long restaurantId, Menu menu) {
if(this.restaurantRepository.findById(restaurantId).isPresent()) {
menu.setRestaurant(Restaurant.builder().id(restaurantId).build());
menu.getMenuCourses().forEach(menuCourse -> {
if (this.categoryRepository.findCategoryByName(menuCourse.getCategory().getName()).isPresent()) {
menu.addCourse(menuCourse); <-- List modification is here
menuCourse.getProductsInCourse().forEach(productInCourse -> {
if (this.productRepository.findProductByIdAndRestaurantId(
productInCourse.getProduct().getId(),
restaurantId)
.isPresent()) {
menuCourse.addProductInMenu(productInCourse); <-- List modification is here
} else {
throw new UnknownResourceException("Unknown Product '" + productInCourse.getProduct().getId() + "'");
}
});
} else {
throw new UnknownResourceException("Unknown Category '" + menuCourse.getCategory().getName() + "'");
}
});
menuRepository.save(menu);
} else {
throw new UnknownResourceException("Unknown Restaurant '" + restaurantId + "'");
}
return menu;
我的问题是,我有嵌套循环,我不知道如何制定一个优雅的解决方案来进行这些检查。我试过使用streams,但我没有做任何接近工作的事情。谢谢你帮忙!
发布于 2020-06-10 15:37:21
我找到的解析嵌套列表而不存在并发问题的唯一解决方案是使用索引而不是迭代器。我对这个解决方案并不十分满意,我很确定还有一些更优雅的事情要做,但不幸的是,我无法弄清楚。
@Override
public Menu saveMenu(Long restaurantId, Menu menu) {
Restaurant restaurant = this.restaurantRepository
.findById(restaurantId)
.orElseThrow(() ->
new UnknownResourceException("Unknown Restaurant '" + restaurantId + "'")
);
log.info("Restaurant id '{}' found", restaurant.getId());
menu.setRestaurant(restaurant);
for (int i = 0; i < menu.getMenuCourses().size(); i++) {
MenuCourse menuCourse = menu.getMenuCourses().get(i);
String categoryName = menuCourse.getCategory().getName();
Category category = this.categoryRepository
.findCategoryByName(categoryName)
.orElseThrow(() ->
new UnknownResourceException("Unknown Category '" + categoryName + "'")
);
log.info("Category name '{}' found", category);
menuCourse.setMenu(menu);
menuCourse.setCategory(category);
menu.getMenuCourses().set(i, menuCourse);
for (int j = 0; j < menuCourse.getProductsInCourse().size(); j++) {
ProductInCourse productInCourse = menuCourse.getProductsInCourse().get(j);
Product product = this.productRepository
.findProductByIdAndRestaurantId(
productInCourse.getProduct().getId(),
restaurantId)
.orElseThrow(() ->
new UnknownResourceException("Unknown Product '" + productInCourse.getProduct().getId() + "'")
);
log.info("Product id '{}' for restaurant '{}' found", product.getId(), restaurantId);
productInCourse.setProduct(product);
productInCourse.setMenuCourse(menuCourse);
menuCourse.getProductsInCourse().set(j, productInCourse);
}
}
return menuRepository.save(menu);
}
我对更好的解决方案很感兴趣!
https://stackoverflow.com/questions/62297768
复制相似问题