假设有两个表 PRODUCT 和 VENDOR。它是 OnetoMany 映射(基于注释)。有一个名为 PRODUCT_VENDOR 的中间表。(产品到供应商是一对多)
供应商在产品创建之前创建。当要创建产品时,用户需要从现有数据中选择供应商。
我的问题是当我要再次保存产品对象(设置了供应商)时,数据将插入到供应商表中。(此时只需要向产品表和中间表插入数据)。
如何防止再次向 VENDOR 表插入数据?
编码
以下是产品
产品实体
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "journey")
private String journey;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "product_vendor", joinColumns = { @JoinColumn(name = "product_id") }, inverseJoinColumns = { @JoinColumn(name = "vendor_id") })
private Set<Vendor> productVendors = new HashSet<Vendor>(0);
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJourney() {
return journey;
}
public void setJourney(String journey) {
this.journey = journey;
}
public Set<Vendor> getProductVendors() {
return productVendors;
}
public void setProductVendors(Set<Vendor> productVendors) {
this.productVendors = productVendors;
}
}
然后在控制器类中。(我使用 Spring 作为业务层)
@Controller
public class ProductController {
@Autowired
ProductService productService;
@Autowired
ProductFormValidator productFormValidator;
@Autowired
VendorService vendorService;
@RequestMapping(value = "/createProduct", method = RequestMethod.GET)
public ModelAndView createProduct() {
ModelAndView mav = new ModelAndView("CreateProduct");
Product product = new Product();
List<Vendor> allVendors = vendorService.getAllVendors();
mav.addObject("product", product);
mav.addObject("allVendors", allVendors);
return mav;
}
@RequestMapping(value = "/createProduct", method = RequestMethod.POST)
public String createProduct(@ModelAttribute("product") Product product,
BindingResult result,@RequestParam("allVendors") String[] vendors) {
if(vendors != null && vendors.length > 0){
Set<Vendor> allVendors = new HashSet<Vendor>();
for(String id : vendors){
System.out.print("##### vendor id ### = "+id);
Vendor vendor = new Vendor();
vendor.setId(Integer.parseInt(id));
allVendors.add(vendor);
}
product.setProductVendors(allVendors);
}
productFormValidator.validate(product, result);
if (result.hasErrors()) {
System.out.println(result.getFieldErrors());
System.out.println(result.getGlobalErrors());
return "CreateProduct";
}
System.out.println("### createProduct in Controller ## ");
productService.createProduct(product);
return "redirect:createProduct.do";
}
}
并在视图中。(jsp)
<%@ include file="header.jsp"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Create Product</title>
</head>
<body>
<h4>Create Product</h4>
<form:form commandName="product" method="POST"
action="createProduct.do">
<fieldset>
<legend>Basic details</legend>
<ul>
<li><label for=name>Name</label> <form:input path="name"
type="text" required="true" placeholder="Name" />
<form:errors path="name" cssStyle="color:red"></form:errors></li>
<li><label for=journey>Journey</label> <form:input
path="journey" type="text" required="true" placeholder="Journey" />
<form:errors path="journey" cssStyle="color:red"></form:errors></li>
<li><label for=vendor>Vendor</label>
<c:forEach var="vendor" items="${allVendors}">
<input type="checkbox" name="allVendors" value="${vendor.id}"/>${vendor.name}
</c:forEach>
<form:errors path="journey" cssStyle="color:red"></form:errors></li>
</ul>
</fieldset>
<fieldset>
<button type=submit>Save Product</button>
</fieldset>
</form:form>
</body>
</html>
最后在dao层,
public void createProduct(Product product) {
sessionFactory.getCurrentSession().save(product);
System.out.println("# product name = "+product.getName());
System.out.println("# productDaoImpl #### New product Created #### ");
}
谢谢。