Chamith Malinda
Chamith Malinda

Reputation: 4529

Hibernate how to save without updating the second table?

Let say there is two tables PRODUCT and VENDOR. And It is OnetoMany mapping (annotation based). There is an intermediate table called PRODUCT_VENDOR. (product to vendor is one to many)

Vendors created preier to product creation. And when going to create a product user need to select the vendors from existing data.

My problem is when I am going to save the product object (with the vendors set) again the data is inserting to vendor table. (In this point in need only insert data to product table and intermediate table).

How can I prevent insert data to VENDOR table again ?

Coding

Following is the Product

Product entity

@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;
    }

}

Then In the controller class. (I'm using 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";
    }

}

And in the view. (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>

Finally in the dao layer,

public void createProduct(Product product) {
   sessionFactory.getCurrentSession().save(product);
   System.out.println("# product name =  "+product.getName());
   System.out.println("# productDaoImpl #### New product Created #### ");       

}

Thanks.

Upvotes: 0

Views: 231

Answers (1)

Chamith Malinda
Chamith Malinda

Reputation: 4529

I resolved the problem. We have to put

@OneToMany(cascade = CascadeType.MERGE)

Upvotes: 1

Related Questions