I am creating my API in Spring Boot. I have that a product is composed of components:
Here is my code: (Entity Layer)
import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;
@Table
@Entity
public class Product{
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany
@JoinColumn
private List<Component> productComponents = new ArrayList<>();
//default constructor
public Product(){
}
public Product(Long id, String name, List<Component> productComponents){
this.id = id;
this.name = name;
this.productComponents = productComponents;
}
//getters
public Long getId(){
return this.id;
}
public String getName(){
return this.name;
}
public List<Component> getProductComponents(){
return this.productComponents;
}
//setters
public void setId(Long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setProductComponents(List<Component> productComponents){
this.productComponents = productComponents;
}
public void addProductComponent(Component component) {
this.productComponents.add(component);
}
public void removeProductComponent(Component component) {
this.productComponents.remove(component);
}
}
My Question:
In the controller class should I change the update method to be as follows, to have the following code, product.addProduct(myComponent) or should I have it as follows: product.setProductComponents((Component) myProduct.getProductComponents()) :
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/api/products/product")
public class ProductController {
private static ProductRepository myProductRepository;
@PutMapping("/update/{id}")
public Product updateProduct(@RequestBody Product myProduct, @RequestBody Component myComponent, @PathVariable Long id){
return myProductRepository.findById(id).map((product) ->{
product.setName(myProduct.getName());
//updated code
product.setProductComponents(myProduct.getProductComponents());
product.addProductComponent(myComponent);
///////
return myProductRepository.save(product);
}).orElseGet(() ->{
myProduct.setId(id);
return myProductRepository.save(myProduct);
});
}
or is there a better solution?
I would suggest to have
public List<Component> getProductComponents(){
return this.productComponents;
}
public void setProductComponents(List<Component> productComponents){
this.productComponents = productComponents;
}
Plus for convenience:
public void addProductComponent(Component component) {
this.productComponents.add(component);
}
public void removeProductComponent(Component component) {
this.productComponents.remove(component);
}
I prefer first one. Reason being
product.setProductComponents((Component) myProduct.getProductComponents())
getProductComponents
well return list I assume. Do this will not work
Better idea is to keet getter and setter dumbest. Advantage is you can omit them in future with libraries like lombok
And you can always add utility methods
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.