I have 3 tables in my database - Booking, Restaurant and 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). 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:
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:
<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:
@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.
Any help is appreciated!
This is a pretty informative error message. Primitive types cannot be null. So if you're performing a load from the DB, and the field doesn't have a NOTNULL constraint, you can have null values, which Hibernate will attempt to assign to your variable of primitive type.
The JPA specification defines that during ordering, NULL values shall be handled in the same way as determined by the SQL standard. The standard specifies that all null values shall be returned before or after all non-null values. It's up to the database to pick one of the two options.
You could use a string instead of boolean and set it to "False", "True" or "" where the empty string represents your null value. Alternatively you need for every Boolean an extra Boolean like <attribute>IsSpecified which you set to false if the attribute value false means null.
The problem with your code is that tableNumber field in Booking.java is of type int but in your jsp you have the following.
<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. 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.
<form:option value="-1" label="--- Select ---" />
2> Other option is to declare the tableNumber field to be of type Integer instead of int.
You have to declare fields as Object type(Integer, Long... etc) instead of primitive type(int, long... etc) in your Value object.
There is no "null" in primitive type. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With