spring mvc jparepository一对多

时间:2013-10-13 02:20:41

标签: spring-mvc

我在产品和类别类之间有一对多的关系,我想添加带有从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>

1 个答案:

答案 0 :(得分:1)

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

productRepository.save(operationService.getProduct());
productRepository.flush();