首页 > 解决方案 > 如何以 JSP 形式插入外键以将其存储在数据库中?

问题描述

这是一个 spring mvc 应用程序,这是它的一部分。当我将类别 ID插入表单时,它会将我移至此错误:

图片

但是当我从表单中删除类别 id输入时,将数据插入到数据库中。

产品.java

@Entity
@Table(name = "product", catalog = "flowershop")
public class Product implements java.io.Serializable {

private Integer id;
private Category categoryid;
private String name;
private Double price;

public Product() {
}

public Product(Category categoryid, String name, Double price) {
    this.categoryid = categoryid;
    this.name = name;
    this.price = price;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "categoryid",foreignKey = @ForeignKey(name = "Product_categoryid_productid"))
public Category getCategoryid() {
    return this.categoryid;
}

public void setCategoryid(Category categoryid) {
    this.categoryid = categoryid;
}

@Column(name = "name", length = 45)
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}

@Column(name = "price", precision = 22, scale = 0)
public Double getPrice() {
    return this.price;
}
public void setPrice(Double price) {
    this.price = price;
}
}

类别.java

@Entity
@Table(name="category", catalog="flowershop")
public class Category  implements java.io.Serializable {


 private Integer id;
 private String name;
 private Set<Product> products = new HashSet<Product>(0);

public Category() {
}
public Category(String name, Set<Product> products) {
   this.name = name;
   this.products = products;
}

@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Integer getId() {
    return this.id;
}
public void setId(Integer id) {
    this.id = id;
}

@Column(name="name", length=45)
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}

@OneToMany(fetch=FetchType.LAZY, mappedBy="categoryid")
public Set<Product> getProducts() {
    return this.products;
}
public void setProducts(Set<Product> products) {
    this.products = products;
}

}

ProductDAOImp.java

@Repository("productDAO")
public class ProductDAOImp implements ProductDAO {

@Autowired
private SessionFactory sessionFactory;

@Override
public void newProduct(Product product) {

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.save(product);
    tx.commit();
    session.close();
}

ProductServiceImp.java

@Service("productService")
@Transactional
public class ProductServiceImp implements ProductService {

@Override
public void newProduct(Product product) {

    productDAO.newProduct(product);
}
}

产品控制器.java

@Controller
@Transactional
@RequestMapping("/product")
public class ProductController {

@Autowired
private CategoryService categoryService;

@Autowired
private ProductService productService;

@RequestMapping(value = "category/{id}", method = RequestMethod.GET)
public String category(@PathVariable("id") Integer id, ModelMap modelMap) {

    Category category = categoryService.find(id);
    Hibernate.initialize(category.getProducts());

    modelMap.put("products", category.getProducts());
    return "product.category";
}

@RequestMapping(value = {"newProduct"}, method = RequestMethod.GET)
public String newProduct(ModelMap modelMap) {

    Product product = new Product();

    modelMap.put("newProduct", product);
    modelMap.put("title", "New Product");
    return "product.newProduct";
}

@RequestMapping(value = {"newProduct"}, method = RequestMethod.POST)
public String newProduct(
        @ModelAttribute("newProduct") Product product,
        ModelMap modelMap) {

    modelMap.put("newProduct", product);

    try {

        productService.newProduct(product);
        return "product.newProduct";
    } catch (Exception e) {

        return "product.newProduct";
    }
}
}

新产品.jsp

<f:form method="POST" modelAttribute="newProduct" action="${pageContext.request.contextPath}/product/newProduct.html">
            <div class="form_row">
                <label class="contact"><strong>Name: </strong></label>
                <input name="name" type="text" class="contact_input"/>
            </div>
            <div class="form_row">
                <label class="contact"><strong>Price: </strong></label> 
                <input name="price" type="text" class="contact_input"/>
            </div>

            <div class="form_row">
                <label class="contact"><strong>Category Id: </strong></label>
                <input name="categoryid" type="text" class="contact_input"/>
            </div>
            <div class="form_row">
                <input type="submit" class="register" value="register">
            </div>
</f:form>

标签: javamysqlhibernatespring-mvc

解决方案


这个注解@GeneratedValue(strategy = IDENTITY)会自动分配一个 id 来记录。因此,在您希望使用手动生成的 id 的项目中,请删除它。


推荐阅读