null值无法分配给基本类型错误(Spring / Hibernate)

时间:2016-04-02 04:01:10

标签: java html mysql spring hibernate

我的数据库中有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

我不知道它为什么会出现这个错误,因为我要添加的表不是空的(至少我认为)。希望你明白我的意思。

我在数据库中的预订表:

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 */;

数据库中的我的餐厅表:

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方法没有做任何事情,因为它甚至在此之前就给我一个错误。

感谢任何帮助!

2 个答案:

答案 0 :(得分:5)

您的代码存在的问题是Booking.java中的tableNumber字段是int类型,但是在您的jsp中,您有以下内容。

<form:option value="" label="--- Select ---" />

这基本上意味着select下拉列表中的值可以为null,在这种情况下,Spring无法将null转换为基本类型,因此它会在页面加载本身上抛出此类异常。这个问题有两种可能的解决方案:

1&GT;将值从值=“”更改为使用某个数字,如value =“ - 1”作为空白选项,并相应地处理应用程序逻辑中的-1。

<form:option value="-1" label="--- Select ---" />

2 - ;其他选项是将tableNumber字段声明为Integer类型而不是int。

答案 1 :(得分:3)

您必须在Value对象中将字段声明为Object type(Integer,Long ...等)而不是基本类型(int,long ...等)。

原始类型中没有“null”。您似乎正在将null值映射到值对象中的原始字段,因此Spring无法为您转换它。