Hibernate如何保存而不更新第二个表?

Hibernate如何保存而不更新第二个表?,hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,假设有两张表,分别是产品表和供应商表。它是一个多域映射(基于注释)。有一个称为PRODUCT_VENDOR的中间表。 (产品对供应商是一对多) 供应商在产品创建之前创建。在创建产品时,用户需要从现有数据中选择供应商 我的问题是,当我要再次保存产品对象(设置了供应商)时,数据将插入到供应商表中。(此时只需将数据插入产品表和中间表) 如何防止再次向供应商表插入数据 编码 以下是产品 产品实体 @Entity @Table(name = "product") public class Product

假设有两张表,分别是产品表和供应商表。它是一个多域映射(基于注释)。有一个称为PRODUCT_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;
    }

}
}


谢谢。

我解决了这个问题。我们必须把

@OneToMany(cascade = CascadeType.MERGE)

使用Cascade属性来限制不同的操作。感谢我使用@OneToMany(Cascade=CascadeType.ALL)@JoinTable(name=“product\u vendor”,joinColumns={@JoinColumn(name=“product\u id”)},inverseJoinColumns={@JoinColumn(name=“vendor\u id”)})但它将再次插入供应商表。请将代码以正确的格式放入原始帖子中?不应该发生的事情更容易理解。错误存在于获取产品、将产品添加到产品以及插入产品的代码中。给我们看看这个代码。也就是说,我认为你应该有很多联系:供应商通常销售几种产品。不应该有级联。我会把完整的代码。谢谢
<%@ 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>
public void createProduct(Product product) {
   sessionFactory.getCurrentSession().save(product);
   System.out.println("# product name =  "+product.getName());
   System.out.println("# productDaoImpl #### New product Created #### ");       
@OneToMany(cascade = CascadeType.MERGE)