【发布时间】:2016-07-22 00:14:57
【问题描述】:
我的数据库中有 3 个表 - Booking、Restaurant 和 RestaurantTable。现在我正在尝试创建一个新的预订,其中一个步骤是添加一张桌子。当我尝试添加此表时,出现以下错误:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.persistence.Column int for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type
我不知道为什么会出现该错误,因为我要添加的表不为空(至少我认为)。希望你明白我的意思。
我在数据库中的 Booking 表:
CREATE TABLE `booking` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`R_id` int(11) NOT NULL,
`date` date not null,
`start` TEXT,
`duration` float(3,1),
`amount_of_people` int,
`name` TEXT,
`contact_preference` TEXT,
`phone_number` TEXT,
`comments` TEXT,
`current_datetime` datetime default current_timestamp,
`new_date` datetime default current_timestamp,
`deleted` bool default false,
`edited` bool default false,
`table_number` int,
PRIMARY KEY (`id`),
FOREIGN KEY (`R_id`) references `restaurant`(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
我的 RestaurantTable 在数据库中:
CREATE TABLE `restaurant_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table_size` int,
`table_number` int,
`restaurant_id` int(11),
PRIMARY KEY (`id`),
FOREIGN KEY (`restaurant_id`) references `restaurant`(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
我尝试添加到预订的表格的 .jsp 文件:
<body>
<jsp:include page="../fragments/menu.jsp"/>
<div id="body">
<h2>Create new booking</h2>
<form:form method="POST" modelAttribute="booking" >
<table>
<tr>
<td>Choose a table*:</td>
<td><form:select path="tableNumber">
<form:option value="" label="--- Select ---" />
<form:options items="${tables}" itemValue="tableNumber" itemLabel="tableNumber"/>
</form:select>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
<div>
<a href="/bookings">Back to List</a>
</div>
</div>
<jsp:include page="../fragments/footer.jsp"/>
</body>
BookingController.java中的相关方法:
@RequestMapping(value = "booking/create/{id}", method = RequestMethod.GET)
public String chooseTable(@PathVariable Long id, Model model) {
Booking booking = bookingService.getBooking(id);
Restaurant restaurant = booking.getRestaurant();
Set<RestaurantTable> tableSet = restaurant.getTable();
model.addAttribute("tables", tableSet);
model.addAttribute("booking", booking);
return "chooseTable";
}
@RequestMapping(value = "booking/create/{id}", method = RequestMethod.POST)
public String chooseTableAction(@PathVariable Long id, Model model) {
return "redirect:/bookings";
}
现在预订控制器中的 POST 方法没有做任何事情,因为它甚至在此之前就给了我一个错误。
感谢任何帮助!
【问题讨论】:
标签: java html mysql spring hibernate