Skip to content
Advertisement

Is there way to set default as null for SQL parameter?

I have a code that creates sql parameters using MapSqlParameterSource. Here is my code:

JavaScript

Basically, if account type is spoofer, I have to have user id instead of account id. However, I don’t like that I have to set account_id and user_id to null when I instantiate parameters. Is there way to set account_id and user_id as null so I don’t have to write this two lines?:

JavaScript

Here is my sql query:

JavaScript

Update:

Maybe my question was not specific enough. What happens is that when I run

JavaScript

With the given parameters without making them “NULL”, I get this exception in my unit test:

JavaScript

I use hsql to test it. my .sql looks like this:

JavaScript

So my specific question is that my test is giving me exception when I try to run test with parameters without setting them to null.

Advertisement

Answer

You must include the addValue(ACCOUNT_ID, null) and addValue(USER_ID, null) because your INSERT statement includes the two named parameters :account_id, :user_id.

The framework attempts to extract the values for the named parameters from the MapSqlParameterSource object and when it does not find one of them, it throws the exception. It does this to avoid user errors, because if you didn’t intend to provide a value for a parameter, you wouldn’t include the parameter in the INSERT statement.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement