【发布时间】:2021-08-01 00:03:43
【问题描述】:
我正在为我的 API 使用 Spring Boot。
这是我的代码: 这是我的 entity 类:
@Table
@Entity
public class Product{
@Id
@GeneratedValue
private Long id;
@ElementCollection
private List<Long> productComponents = new ArrayList<>();
public void addProductComponent(Long myComponent){
this.productComponents.add(myComponent);
}
}
这是我的控制器方法:
@PostMapping("/components/add")
public void addComponentToProductComponents(@PathVariable Long productId, @PathVariable Long componentId) throws ProductException {
var myProduct = myProductRepository.findById(productId);
myProduct.map((item) -> {
item.addProductComponent(componentId);
});
}
在我的“addComponentToProductComponents”控制器方法中,我要做的是通过 id 选择产品,这就是我在这里所做的:
var myProduct = myProductRepository.findById(productId);
然后:
我的问题:
通过id选择产品后如何将componentId添加到ListproductComponents>?
【问题讨论】:
标签: java spring spring-boot spring-mvc spring-data-jpa