Quantcast

How can I save null into database which the column is int ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How can I save null into database which the column is int ?

tomcatlee
Please check the following code:  

<g:select  id="period" name="period" optionKey="key" optionValue="value" from="[[key:0,value:'Please choose'],[key:'1',value:'everyday']]" value="${remindInstance?.period}" ></g:select>

I want to save NULL to database when user didn't choose peroid, as you can see,the default value is 0,
In controller I add this code:

if(Integer.parseInt(params.period)==0){
              params.period=null
}
but I can't save, the error msseage is :

Failed to convert property value of type [null] to required type [int] for property period; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [int] for property period: PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value

In database this column is nullable.

Any suggestion?
Thank you.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I save null into database which the column is int type?

burtbeckwith
Change it to Integer in the domain class.

Burt

>
> Please check the following code:  
>
> <g:select  id="period" name="period" optionKey="key" optionValue="value"
> from="[[key:0,value:'Please choose'],[key:'1',value:'everyday']]"
> value="${remindInstance?.period}" ></g:select>
>
> I want to save NULL to database when user didn't choose peroid, as you can
> see,the default value is 0,
> In controller I add this code:
>
> if(Integer.parseInt(params.period)==0){
>               params.period=null
> }
> but I can't save, the error msseage is :
>
> Failed to convert property value of type [null] to required type [int] for
> property period; nested exception is java.lang.IllegalArgumentException:
> Cannot convert value of type [null] to required type [int] for property
> period: PropertyEditor
> [org.springframework.beans.propertyeditors.CustomNumberEditor] returned
> inappropriate value
>
> In database this column is nullable.
>
> Any suggestion?
> Thank you.
>

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I save null into database which the column is int type?

tomcatlee
Great! It works.
Thanks
Burt Beckwith wrote
Change it to Integer in the domain class.

Burt

>
> Please check the following code:  
>
> <g:select  id="period" name="period" optionKey="key" optionValue="value"
> from="[[key:0,value:'Please choose'],[key:'1',value:'everyday']]"
> value="${remindInstance?.period}" ></g:select>
>
> I want to save NULL to database when user didn't choose peroid, as you can
> see,the default value is 0,
> In controller I add this code:
>
> if(Integer.parseInt(params.period)==0){
>               params.period=null
> }
> but I can't save, the error msseage is :
>
> Failed to convert property value of type [null] to required type [int] for
> property period; nested exception is java.lang.IllegalArgumentException:
> Cannot convert value of type [null] to required type [int] for property
> period: PropertyEditor
> [org.springframework.beans.propertyeditors.CustomNumberEditor] returned
> inappropriate value
>
> In database this column is nullable.
>
> Any suggestion?
> Thank you.
>

 
Loading...