2013-10-13 3 views
0

у меня есть отношения один-ко-многим между продуктом и категории класса, я хочу добавить продукт с CATEGORY_ID который выбрал из Jsp страницы, но при выполнении этого кода обновления категории таблицы обнулить Spring MVC jparepository один ко многим

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name="productId") 
private int productId; 
@NotEmpty 
@Column(name="productName",unique=true) 
private String productName; 
@NotEmpty 
@Column(name="description") 
private String description; 
@Column(name="price") 
private int price; 
@JoinColumn(name = "category_id") 
@ManyToOne(cascade = CascadeType.ALL) 
Category category; 
@OneToMany(mappedBy="product") 
Collection<OrderedProduct> orderedProductCollection; 

public Collection<OrderedProduct> getOrderedProductCollection() { 
    return orderedProductCollection; 
} 
public void setOrderedProductCollection(
     Collection<OrderedProduct> orderedProductCollection) { 
    this.orderedProductCollection = orderedProductCollection; 
} 
public int getProductId() { 
    return productId; 
} 
public void setProductId(int productId) { 
    this.productId = productId; 
} 
public String getProductName() { 
    return productName; 
} 
public void setProductName(String productName) { 
    this.productName = productName; 
} 
public String getDescription() { 
    return description; 
} 
public void setDescription(String description) { 
    this.description = description; 
} 
public int getPrice() { 
    return price; 
} 
public void setPrice(int price) { 
    this.price = price; 
} 
public Category getCategory() { 
    return category; 
} 
public void setCategory(Category category) { 
    this.category = category; 
} 



    } 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name="categoryId") 
private int categoryId; 
@Column(name="categoryName") 
private String categoryName; 
@OneToMany(mappedBy="category") 
private List<Product> productlist=new ArrayList<Product>(); 
public Category() { 
    super();   
}      
public Category(int categoryId) { 
    super(); 
    this.categoryId = categoryId; 
} 


public Category(int categoryId, String categoryName) { 
    super(); 
    this.categoryId = categoryId; 
    this.categoryName = categoryName; 
} 



public int getCategoryId() { 
    return categoryId; 
} 
public void setCategoryId(int categoryId) { 
    this.categoryId = categoryId; 
} 
public String getCategoryName() { 
    return categoryName; 
} 
public void setCategoryName(String categoryName) { 
    this.categoryName = categoryName; 
} 
public List<Product> getProductlist() { 
    return productlist; 
} 
public void setProductlist(List<Product> productlist) { 
    this.productlist = productlist; 
}} 

public ModelAndView addProduct(@Valid OperationService operationService,BindingResult result,ModelMap model,ModelAndView mav) 
{ 
    if(result.hasErrors()) 
    { 
     mav.setViewName("addProduct"); 
     return mav; 
    } 
    operationService.getProduct().setCategory(operationService.getCategory()); 
    productRepository.save(operationService.getProduct()); 
    mav.setViewName("success"); 
    return mav; 
} 

public class OperationService { 
Customer customer; 
CustomerCart customerCart; 
OrderedProduct orderedProduct; 
Product product; 

Category category; 
public Customer getCustomer() { 
    return customer; 
} 
public void setCustomer(Customer customer) { 
    this.customer = customer; 
} 
public CustomerCart getCustomerCart() { 
    return customerCart; 
} 
public void setCustomerCart(CustomerCart customerCart) { 
    this.customerCart = customerCart; 
} 
public OrderedProduct getOrderedProduct() { 
    return orderedProduct; 
} 
public void setOrderedProduct(OrderedProduct orderedProduct) { 
    this.orderedProduct = orderedProduct; 
} 
public Product getProduct() { 
    return product; 
} 
public void setProduct(Product product) { 
    this.product = product; 
} 
public Category getCategory() { 
    return category; 
} 
public void setCategory(Category category) { 
    this.category = category; 
}} 

Вот страница JSP, выберите значение формы category_id категории таблицы

 <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Add Product</title> 
    </head> 
    <body> 
    <form:form action="AddProduct" method="post" commandName="operationService"> 
    <form:select path="category.categoryId"> 
    <c:forEach var="cate" items="${allCategory}"> 
    <form:option value="${cate.categoryId}" id="{cate.categoryId}"> 
    ${cate.categoryName}</form:option> 
    </c:forEach> 
    </form:select> 
    33<form:input path="product.productName"/> 
    44<form:input path="product.description"/> 
    55<form:input path="product.price"/> 
    <input type="submit"> 
    </form:form> 
    <a href="AddCategory">Add Category</a> 
    </body> 
    </html> 
    </i> 

ответ

1

Я думаю, вам не хватает вызова flush() в репозитории. Попробуйте следующее:

productRepository.save(operationService.getProduct()); 
productRepository.flush(); 
Смежные вопросы