I created a model with a recursive relation, but when I tray to show it in a form:select
in a JSP view, it returns an error about datatype conversions, how can I show recursive model in a JSP view?
I'm using Java 1.8 and spring-core 4.1.
When I show a database row, that the recursive field is null
, it works, but when I try to show a row with the recursive field filled with a valid value, it doesn't works and generate the error.
this is the model field from the class Category
@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(nullable=true)
private Category subCategory;
my controller send to the JSP view, a Category object named "category"
modelAndView.addObject("category", category);
in the view I'm showing the object by a form:select component
<form:select path="subCategory.id"
id="category_subCategory"
multiple="false"
cssClass="form-control">
<form:option value="null">-</form:option>
<form:options items="${categories}"
itemValue="id"
itemLabel="name"/>
</form:select>
Instead of displaying the component, it's breaking and generating the error below:
Jun 26, 2019 4:03:09 PM org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet [jsp] threw exception java.lang.NumberFormatException: For input string: "null" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)...
Jun 26, 2019 4:03:09 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/ecommerce] threw exception [javax.servlet.ServletException: javax.servlet.jsp.JspException: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.persistence.Id @javax.persistence.GeneratedValue java.lang.Integer for value 'null'; nested exception is java.lang.NumberFormatException: For input string: "null"] with root cause java.lang.NumberFormatException: For input string: "null"...