com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'ID_category'不能为空
我试图在表格“product”和“category”之间建立连接是一对多的。但是对象产品中的类别条目,它不会被填充。为什么?
@Entity
@Table(name = "CATEGORY")
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "name")
private String name;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "category")
private Set<Product> products = new HashSet<Product>(0);
public Category() { }
@Entity
@Table(name = "PRODUCTS")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "ID_PRODUCT")
private int id;
@Column(name = "name")
private String name;
@ManyToOne
@JoinColumn(name = "ID_category", nullable = true)
private Category category;
@ManyToOne
@JoinColumn(name = "ID_producer", nullable = true)
private Producer producer;
@Column(name = "description")
private String description;
@Column(name = "price")
private int price;
@Column(name = "number")
private Integer number;
@Column(name = "picture")
private String picture;
这是我的更新控制器。我更新了发现的产品。
@Controller
public class ProductUpdateController {
private static final Logger log = Logger.getLogger(ProductUpdateController.class);
@Autowired
private ProductService productService;
@Autowired
private CategoryService categoryService;
@Autowired
private ProducerService producerService;
@RequestMapping(value = "/products/{id}/update.html", method = RequestMethod.GET)
public String updateCourse(Model model, HttpSession session, HttpServletRequest request,
@PathVariable("id") Integer productId) {
Product product = (Product) productService.findProductById(productId);
model.addAttribute(product);
return "update"; }
@RequestMapping(value = "/products/{id}/update.html", method = RequestMethod.POST)
public String updateCoursePost(Model model, HttpSession session, HttpServletRequest request,
@PathVariable("id") Integer productId, @Valid Product product, BindingResult result)
throws AddressException, Exception {
product = productService.updateProduct(product);
这是我的观看页面。
<form:form name='updateForm' commandName="product" onsubmit="return validate(this); " method="POST">
<fieldset>
<div class="form-group">
<label class="control-label">Name</label>
<div class="controls">
<form:input path="name" />
</div>
</div>
<div class="form-group">
<label class="control-label">Category</label>
<div class="controls">
<select name="category" class="span3">
<c:forEach var="cat" items="${listCategories}">
<option value="${cat}">${cat.name}</option>
</c:forEach>
</select>
</div>
</div>
这是我对这些表的sql脚本。
CREATE TABLE `category` (
`ID` int(11) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `category` (`ID`, `name`) VALUES
(1, 'Sedan'),
(2, 'SUV'),
(3, 'Hatchback'),
(4, 'Crossover'),
(5, 'Minibus');
CREATE TABLE `producer` (
`ID` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`links` varchar(30) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `producer` (`ID`, `name`, `links`) VALUES
(1, 'Nissan', 'nissan.com'),
(2, 'Porsche', 'porsche.com'),
(5, 'Bentley', 'bentley.com');
CREATE TABLE `products` (
`ID_PRODUCT` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`ID_category` int(11) NOT NULL,
`ID_producer` int(11) NOT NULL,
`description` varchar(30) NOT NULL,
`price` int(11) NOT NULL,
`number` int(11) NOT NULL DEFAULT '10',
`picture` varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `products` (`ID_PRODUCT`, `name`, `ID_category`, `ID_producer`, `description`, `price`, `number`, `picture`) VALUES
(1, 'Nissan 350z', 1, 1, '2.0', 30000, 10, '2009');
答案 0 :(得分:0)
这似乎与关系有关。 Product表有一个外键category_id,即使你将它注释为可空,它也不能在任何时候为空。