表类别和产品是按一对多关系映射的,但是我却遇到这样的错误:
Caused by: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`summer-project`.`product`, CONSTRAINT `product_category` FOREIGN KEY (`category`) REFERENCES `categories` (`category_code`) ON DELETE NO ACTION ON UPDATE NO ACTION)
现在,当我提交添加产品表单时,应该使用类别表中的外键将数据添加到产品表中。但是我收到了SQL错误。
这是我的addproducts表单:
<form action="#" th:action="@{/products/insert}"
th:object="${products}" method="POST" enctype="multipart/form-data">
<!-- this is for the update so that the categories id will be sent along the form -->
<input type="hidden" th:field="*{id}">
<input type="text" th:field="*{productName}"
class="form-control mb-4 col-4" placeholder="Name">
<input type="text" th:field="*{price}"
class="form-control mb-4 col-4" placeholder="Price">
<input type="text" th:field="*{specs}"
class="form-control mb-4 col-4" placeholder="Specs">
<!-- for drop downlist to select the category -->
<select th:field="*{categories}" class="form-control mb-4 col-2">
<option value="0">Select Category</option>
<option th:each="tempcategory:${categories}"
th:text="${tempcategory.categoryCode}" th:value="${tempcategory.id}" ></option>
</select>
<!-- for uploading the file -->
<input type="file" name="file" id="file">
<button type="submit" class="btn btn-info col-2">Add</button>
</form>
<a th:href="@{/products/list}">Go back to list</a>
</div>
这是我的插入控制器:
@PostMapping("/insert")
public String insertCategory(@ModelAttribute("products") Products products,
@RequestParam("file") MultipartFile file,
BindingResult result) throws IOException {
products.setImagePath("dummy data");
Categories categories = categoriesService.findById(products.getCategories().getId());
categories.add(products);
productService.insertOrUpdate(products);
// System.out.println("-------------------"+categories.getCategoryCode()+"------------------");
return "redirect:/products/list";
}
这是我的产品类别:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="product_name")
private String productName;
@Column(name="product_specs")
private String specs;
@Column(name = "image_path")
private String imagePath;
@Column(name="price")
private double price;
@ManyToOne(fetch = FetchType.LAZY,cascade = {CascadeType.PERSIST,CascadeType.MERGE,
CascadeType.DETACH,CascadeType.REFRESH}) // everyThing except delete
@JoinColumn(name="category")
private Categories categories;
public Products() {
}
public Products(String productName, String imagePath, double price,String specs) {
this.productName = productName;
this.specs = specs;
this.imagePath = imagePath;
this.price = price;
// this.categories =categories;
}
// getters and setters
这是我的类别课程:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="category_name")
private String categoryName;
@Column(name="image_path")
private String imagePath;
@Column(name = "category_code")
private String categoryCode;
@OneToMany(mappedBy = "categories", cascade =
{CascadeType.PERSIST,CascadeType.MERGE,
CascadeType.DETACH,CascadeType.REFRESH}) // it will look for categories in products table to create a relationship
private List<Products> products;
// this is for the relational mapping
public void add(Products tempProduct) {
if(products == null) {
products = new ArrayList<>();
}
products.add(tempProduct);
tempProduct.setCategories(this);
}
// getters and setters
答案 0 :(得分:0)
我知道了 需要在类别类中实现Serializeable
@Entity
@Table(name = "categories")
public class Categories implements Serializable{
//define fields
// define getters and setters
}