简体   繁体   English

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

[英]A null value cannot be assigned to a primitive type error (Spring/Hibernate)

I have 3 tables in my database - Booking, Restaurant and RestaurantTable. 我的数据库中有3个表 - Booking,Restaurant和RestaurantTable。 Right now I am trying to create a new booking and one of the steps there is adding a table. 现在我正在尝试创建一个新的预订,其中一个步骤就是添加一个表格。 When I try to add this table the following error comes up: 当我尝试添加此表时,出现以下错误:

 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

I don't know why it gives that error, since the table I am trying to add is not null (I think at least). 我不知道它为什么会出现这个错误,因为我试图添加的表不是null(至少我认为)。 Hope you understand what I mean. 希望你明白我的意思。

My Booking table in the database: 我在数据库中的预订表:

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

My RestaurantTable in the database: 我在数据库中的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 */;

The .jsp file of the table I am trying to add to the booking: 我想要添加到预订的表的.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>

Relevant methods in BookingController.java: 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";
}

Right now the POST method in the booking controller doesn't do anything since it's giving me an error even before that. 现在预订控制器中的POST方法没有做任何事情,因为它甚至在此之前就给我一个错误。

Any help is appreciated! 任何帮助表示赞赏!

The problem with your code is that tableNumber field in Booking.java is of type int but in your jsp you have the following. 您的代码的问题是Booking.java中的tableNumber字段是int类型,但在您的jsp中,您有以下内容。

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

This basically implies that the value from the select dropdown can be null in which case Spring has no way to convert a null to a primitive type hence it throws such an exception on page load itself. 这基本上意味着select下拉列表中的值可以为null,在这种情况下,Spring无法将null转换为基本类型,因此它会在页面加载本身上抛出此类异常。 There are 2 possible solutions to this issue : 这个问题有两种可能的解决方案:

1> Change the value from value="" to using some number like value="-1" for the blank option and handle the -1 in your application logic accordingly. 1>将值从值=“”更改为使用某个数字(如value =“ - 1”)作为空白选项,并相应地处理应用程序逻辑中的-1。

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

2> Other option is to declare the tableNumber field to be of type Integer instead of int. 2>其他选项是将tableNumber字段声明为Integer类型而不是int。

You have to declare fields as Object type(Integer, Long... etc) instead of primitive type(int, long... etc) in your Value object. 您必须在Value对象中将字段声明为对象类型(Integer,Long ...等)而不是基本类型(int,long ...等)。

There is no "null" in primitive type. 原始类型中没有“null”。 It seems that your are mapping a null value to a primitive field in your value object, so Spring can't convert it for you. 您似乎正在将null值映射到值对象中的原始字段,因此Spring无法为您转换它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在hibernate查询执行时,是否将空值赋给基本类型的属性? - Null value was assigned to a property of primitive type while hibernate query execution? hibernate异常将空值赋给基本类型setter的属性 - hibernate exception Null value was assigned to a property of primitive type setter 避免“Hibernate异常将Null值分配给基本类型setter的属性”而不使用包装器 - Avoid “Hibernate exception Null value was assigned to a property of primitive type setter” without wrappers org.hibernate.PropertyAccessException:空值已分配给布尔类型的属性 - org.hibernate.PropertyAccessException: Null value was assigned to a property of boolean type Hibernate将NULL插入到基本类型字段中 - Hibernate inserting NULL into primitive type field 可选的int参数&#39;&#39;存在但由于被声明为基本类型而无法转换为空值 - Optional int parameter '' is present but cannot be translated into a null value due to being declared as a primitive type 存在可选的int参数&#39;page&#39;,但由于被声明为原始类型,因此无法转换为null值。 - Optional int parameter 'page' is present but cannot be translated into a null value due to being declared as a primitive type. 是否可以为值分配或与值类型变量进行比较? - Can null be assigned to or be compared to a value type variable? @NamedStoredProcedureQuery与Spring Data JPA Repository - Type不能为null错误 - @NamedStoredProcedureQuery with Spring Data JPA Repository - Type cannot be null error 分配给基元的对象类型参考变量 - Object type reference variable assigned to a primitive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM