I have a table in MYSQL and I am using JDBC Templates to do an insert into that table.
One of the columns has a default value, and I am not specifying it in the Map<String, Object> parameters
map.
I am getting an exception Column 'colName' cannot be null
.
Can anyone explain this please?
Thanks
*Edit: code *
contactDetailsInsertTemplate = new SimpleJdbcInsert( dataSource ).withTableName("contactdetails").usingGeneratedKeyColumns("contactcode"); Map<String, Object> parameters = new HashMap<String, Object>(); Number newId = contactDetailsInsertTemplate.executeAndReturnKey(parameters);
Advertisement
Answer
You need to limit the columns specified in the SQL Insert.
See SimpleJdbcInsert.usingColumns()
If you don’t do this, the SQL statement will need values for ALL columns and the default values cannot be used.
e.g. use
insert into mytable (col1, col2) values (val1, val2);
instead of
insert into mytable values (val1, val2);