Hibernate @ManyToOne映射问题

时间:2017-02-22 14:03:52

标签: java spring hibernate hql hibernate-mapping

我有两个表类别和产品。其中category id是products table中的外键。当我尝试使用hql在products表中插入数据时,我的类别表也会更新,并且假设类别是移动的,其id为1而不是更新后它将为1且为null。这是代码。

Products.java

    package com.main.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="Products")
public class Products {
@Id@GeneratedValue(strategy=GenerationType.AUTO)    
private int id;
@Column
private String productname;
@Column
private String productstore;
@Column
private String productdesc;
@Column
private double price;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="categoryid")
private Category category;
private String imagePath;
public Category getCategory() {
    return category;
}
public void setCategory(Category category) {
    this.category = category;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getProductname() {
    return productname;
}
public void setProductname(String productname) {
    this.productname = productname;
}
public String getProductstore() {
    return productstore;
}
public void setProductstore(String productstore) {
    this.productstore = productstore;
}
public String getProductdesc() {
    return productdesc;
}
public void setProductdesc(String productdesc) {
    this.productdesc = productdesc;
}
public double getPrice() {
    return price;
}
public void setPrice(double price) {
    this.price = price;
}
public String getImagePath() {
    return imagePath;
}
public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}


}

Category.java

package com.main.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Category")
public class Category {
    @Id@GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="category_id")
    private int id;
    @Column
    private String categoryName;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }



}

admin.jsp

<form id="asd" action="/shopping_cart/addProduct" method="post">
    <center>
                <h3>Please Enter Product Details</h3>
                  Category<select name="category.id">
                <option name="mobiles" value="1">Mobiles</option>
                <option name="" value="2">Books</option>
                <option name="" value="3">EarPhones</option>
            </select><br /> <br /> 
                Product Name <input type="text" value="" name="productname" width="100%" /><span
                    id="errorname" required></span> <br /> <br /> Product Store &nbsp;
                &nbsp; <input type="text" value="" name="productstore" required /> <span
                    id="error_address" required></span><br /> <br />
                Product Desc<input
                    type="text" value="" name="productdesc" required> <span
                    id="error_city"></span><br /> <br /> Image Path
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
                    type="text" value="" name="imagePath" /><span id="error_state"></span>
                <br /> <br /> Price
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input
                    type="number" value="" name="price" /><span id="error_zipcode"></span>
                <br /> <br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
                    type="submit" value="Add Product" name="addProduct" /> 
            </center>
    </form> 

控制器

@RequestMapping(value = "/addProduct", method = RequestMethod.POST)
    public ModelAndView processForm11(@ModelAttribute("products1") Products products) {
        System.out.println("adding product to db");
        adminService.addProduct(products);
        System.out.println("Product inserted successfully");
        ModelAndView model=new ModelAndView("nextpage");
return model;
    }

DAOImpl类

package com.main.dao;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.main.model.Products;
@Repository
public class AdminDAOImpl implements AdminDAO  {@Resource(name="sessionFactory")
protected SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}
    public void addProduct(Products product) {
        Session session=sessionFactory.openSession();
        Transaction tx=session.beginTransaction();
        session.save(product);
        tx.commit();
        session.close();
    }

}

提前致谢。

0 个答案:

没有答案