0

我在产品和类别类之间有一对多的关系,我想添加具有从 jsp 页面中选择的 category_id 的产品,但是当此代码运行类别表更新为 null

@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>
4

1 回答 1

1

我猜你错过了对存储库的 flush() 调用。尝试这个:

productRepository.save(operationService.getProduct());
productRepository.flush();
于 2014-02-22T00:40:53.787 回答