假设有两个表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 for bussiness layer)
@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 #### ");
}
感谢。
答案 0 :(得分:1)
我解决了这个问题。我们必须把
@OneToMany(cascade = CascadeType.MERGE)