gpt4 book ai didi

java - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Column 'ID_category' cannot be null

转载 作者:行者123 更新时间:2023-11-30 22:05:51 25 4
gpt4 key购买 nike

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列“ID_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 。我更新找到的产品。

 @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');

最佳答案

这似乎是关系的问题。 Product 表有一个外键 category_id,它在任何时候都不能为空,即使您将其注释为可为空。

关于java - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Column 'ID_category' cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41816337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com