我已编写代码来添加产品,但我无法将数据持久保存到db表。要插入数据,我需要考虑产品表和类别表。 类别表具有id和name字段。产品表是我需要插入数据的地方,例如名称,价格,描述和category_id,它是从类别表的id引用的。
我认为问题的关键是类别字段。需要使用 product.setCategory(category); 进行设置 因为它不允许我使用SetcategoryID设置。
所以,我在错误的地方寻求帮助,或者我应该如何更正adminservlet中的以下代码。我已经包含了影响这件事的代码。
更新:在整晚尝试不同的事情后解决了这个问题。您的建议和知识仍然受到欢迎,因为您的代码可能比我的更高效,因为我是Java平台的初学者。还有一件事需要解决,就是在数据插入成功后重定向或转发响应 它不起作用。
AdminServlet
else if (userPath.equals("/admin/addProduct")){
String name = request.getParameter("name");
Double price = Double.parseDouble(request.getParameter("price")) ;
String description = request.getParameter("description");
//Integer category_id = Integer.parseInt(request.getParameter("category_id")) ;
Integer category_id = Integer.parseInt(request.getParameter("category_id")) ;
//Category category = new Category(request.getParameter("category"););
//String category = category(request.getParameter("category"));
boolean validationErrorFlag;
validationErrorFlag = validator.validateForm(name, request);
// if validation error found, return to same
if (validationErrorFlag == true) {
request.setAttribute("validationErrorFlag", validationErrorFlag);
}
else {
try{
request.setAttribute("name" , name);
request.setAttribute("price" , price);
request.setAttribute("description" , description);
// get selected category
//Category category = categoryFacade.find(Integer.parseInt(categoryId));
request.setAttribute("category" , category_id);
productManager.addProduct(name, price, description, category);
response.sendRedirect("/admin/showProduct");
}
catch(Exception ex){
ex.toString();
}
}
}
ProductManager.java
public Product addProduct(String name, Double price, String description, Category category) {
Product product = new Product();
product.setName(name);
//product.setCatId();
product.setCategory(category);
product.setPrice(price);
product.setDescription(description);
em.persist(product);
return product;
}
addProduct.jsp
<h1>Add Product item: </h1>
<form id ="addProductForm" action="<c:url value='/admin/addProduct'/>" method="post">
<c:if test="${!empty validationErrorFlag}">
<p><fmt:message key="validationErrorMessage"/> </p>
<c:if test="${!empty nameError}"><p><fmt:message key="nameError"/></p></c:if>
<c:if test="${!empty categoryError}"><p><fmt:message key="categoryError"/></p></c:if>
<c:if test="${!empty priceError}"><p><fmt:message key="priceError"/></p></c:if>
<c:if test="${!empty descriptionError}"><p><fmt:message key="descriptionError"/></p></c:if>
</c:if>
<p><strong>Product Name:</strong></p>
<input class ="form-control" type="text" name="name" value="${param.name}">
<p><strong>Product Category:</strong></p>
<select id="category" name="category" class="form-control">
<option value="">Select</option>
<c:forEach var="category" items="${categories}" varStatus="iter">
<option value="${category.id}">${category.name}</option>
</c:forEach>
</select>
<p><strong>Product Price:</strong></p>
<input class ="form-control col-md-6" type="text" name="price">
<p><strong>Product details:</strong></p>
<textarea class ="form-control" type="textarea" name="description" rows="4">
</textarea>
<br>
<p><input class ="btn-success" type="submit" value="Submit"></p>
</form>
category.java实体
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
private Collection<Product> productCollection;
Product.java实体
@JoinColumn(name = "category_id", referencedColumnName = "id")
@ManyToOne(optional = false)
private Category category;
/*
@OneToOne
private Category categoryId;
*/
@OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
private Collection<OrderedProduct> orderedProductCollection;
答案 0 :(得分:0)
解决了我的自我。如果有人将来需要,这是解决方案。
public Product addProduct(String name, Double price, String description, Integer category_id) {
Product product = new Product();
product.setName(name);
//product.setCatId();
Category category = new Category(category_id);
product.setCategory(category);
product.setPrice(price);
product.setDescription(description);
//em.persist(category);
em.persist(product);
return product;
}