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);
These problems of JDBC API are eliminated by Spring JDBC-Template. It provides methods to write the queries directly that saves a lot of time and effort. There are a number of options for selecting an approach to form the basis for your JDBC database access. Spring framework provides the following approaches for JDBC database access:
By using the JDBC Template, query the database bypassing three parameters, SQL – query, PreparedStatementSetter – to set the value and FrameworkMapper – to map the data to the respective properties.
Spring Boot provides a starter spring-boot-starter-jdbc for using JDBC with relational databases. As with every Spring Boot starter, this one helps us get our application up and running quickly. 7.1.
All the classes in Spring JDBC are divided into four separate packages: core — the core functionality of JDBC. Some of the important classes under this package include JdbcTemplate, SimpleJdbcInsert, SimpleJdbcCall and NamedParameterJdbcTemplate.
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);
Since the OP is supplying a MAP to the SimpleJdbcInsert
.
This is what I did.
I made sure the MAP only has columns that I want inserted.
I extracted the Map keys and converted them into String[]
like below
Set<String> set = map.keySet();
String[] columnNames = (String[]) map.keySet().toArray(new String[set.size()]);`
When initiating the SimpleJdbcInsert
I passed in the columns I want to be used as below
SimpleJdbcInsert sji = new SimpleJdbcInsert(dataSource).withTableName("table_name");
sji.usingColumns(columnNames);`
Hope it helps.
The problem is somewhere else: when I use SimpleJdbcInsert
and pass a map of parameters to it, I simple expect to create insert statment only with parameters I provided. I have a NOT NULL column in the table with DEFAULT value and I don't want to specify it. I also don't want to explicitly put all column names in usingColumns()
as I've just do it when creating a map of parameters! Why should I do it again? The problem is that SimpleJdbcInsert
want's to be smarter and add all columns to insert statement which is not necessary. Why not create a statement only using columns provided with parameters map?
See: http://forum.spring.io/forum/spring-projects/data/54209-simplejdbcinsert-and-columns-with-default-values
You can add the columns which have a default value as parameters to usingGeneratedKeyColumns()
.
Column names included as parameters of that method will be omitted from the generated INSERT
statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With