我无法在管理页面中添加数据库

时间:2017-11-30 08:37:13

标签: java spring hibernate spring-mvc

[HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: NULL not allowed for column "PRODUCTNAME"; SQL statement][1]


  [1]: https://i.stack.imgur.com/H6NkO.png



 i can not add datbase in admin page so what is my probelem in this code?
请帮助我,我无法理解。那我怎么解决这个问题呢?

我无法在管理页面中添加数据库,那么这段代码中的我的探测器是什么? 请帮帮我,我无法理解。那我怎么解决这个问题呢?

home controller
---------------

 package com.emusicstore.controller;

import com.emusicstore.dao.ProductDao;
import com.emusicstore.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
public class HomeController {

    private Path path;

    @Autowired
    private ProductDao productDao;

    @RequestMapping("/")
    public String home()
    {
        return "home";
    }

    @RequestMapping("/productList")
    public String getProducts(Model model){
        List<Product> products = productDao.getAllProducts();
        model.addAttribute("products",products);

        return "productList";
    }


    @RequestMapping("/productList/viewProduct/{productId}")
    public String viewProduct(@PathVariable String productId, Model model) throws IOException
    {
        Product product = productDao.getProductById(productId);
        model.addAttribute(product);

        return "viewProduct";
    }
    @RequestMapping("/admin")
    public String adminPage()
    {
        return "admin";
    }
    @RequestMapping("/admin/productInventory")
    public String productInventory(Model model)
    {
        List<Product> products = productDao.getAllProducts();
        model.addAttribute("products",products);

        return  "productInventory";
    }

    @RequestMapping("/admin/productInventory/addProduct")
    public String addProduct(Model model){
        Product product = new Product();
        product.setProductCategory("instrument");
        product.setProductCondition("new");
        product.setProductStatus("active");

        model.addAttribute("product",product);

        return "addProduct";

    }

    @RequestMapping(value = "/admin/productInventory/addProduct" , method = RequestMethod.POST)
    public String addProductPost(@ModelAttribute("product") Product product, HttpServletRequest request){
        productDao.addProduct(product);

        MultipartFile productImage = product.getProductImage();
        String rootDirectory = request.getSession().getServletContext().getRealPath("/");
        path = Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\"+product.getProductId()+".png");

        if(productImage != null && !productImage.isEmpty())
        {
            try{
                productImage.transferTo(new File(path.toString()));
            }catch (Exception e){
                e.printStackTrace();
                throw  new RuntimeException("Product image saving failed",e);
            }
        }

        return "redirect:/admin/productInventory";

    }
    @RequestMapping("/admin/productInventory/deleteProduct/{id}")
    public String deleteProduct(@PathVariable String id, Model model){

        productDao.deleteProduct(id);
        return "redirect:/admin/productInventory";
    }


}

product
-------
package com.emusicstore.model;

import org.springframework.web.multipart.MultipartFile;

import javax.persistence.*;

@Entity

public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    private String productId;
    private String productName;
    private String productCategory;
    private String productDescription;
    private double productPrice;
    private String productCondition;
    private String productStatus;
    private int unitInStock;
    private String productManufacturer;


    @Transient
    private MultipartFile productImage;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }



    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductCategory() {
        return productCategory;
    }

    public void setProductCategory(String productCategory) {
        this.productCategory = productCategory;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductCondition() {
        return productCondition;
    }

    public void setProductCondition(String productCondition) {
        this.productCondition = productCondition;
    }

    public String getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(String productStatus) {
        this.productStatus = productStatus;
    }

    public int getUnitInStock() {
        return unitInStock;
    }

    public void setUnitInStock(int unitInStock) {
        this.unitInStock = unitInStock;
    }

    public String getProductManufacturer() {
        return productManufacturer;
    }

    public void setProductManufacturer(String productManufacturer) {
        this.productManufacturer = productManufacturer;
    }

    public MultipartFile getProductImage() {
        return productImage;
    }

    public void setProductImage(MultipartFile productImage) {
        this.productImage = productImage;
    }

}

我无法在管理页面中添加数据库,那么这段代码中的我的探测器是什么? 请帮帮我,我无法理解。那我怎么解决这个问题呢?

我无法在管理页面中添加数据库,那么这段代码中的我的探测器是什么? 请帮帮我,我无法理解。那我怎么解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

我认为您已在产品名称

上应用了NOT NULL约束