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

View: New views
3 Messages — Rating Filter:   Alert me  

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

by tomLee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

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

by Burt Beckwith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

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

by tomLee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.
>