Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Spring's JDBC Templates doesn't use the tables default value

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);
like image 794
Gleeb Avatar asked Dec 10 '13 15:12

Gleeb


People also ask

What is spring JDBC-template?

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:

What is a JDBC template and how to use it?

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.

What is Spring-Boot-starter-JDBC?

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.

What are the classes in spring JDBC?

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.


4 Answers

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);
like image 139
NickJ Avatar answered Sep 18 '22 23:09

NickJ


Since the OP is supplying a MAP to the SimpleJdbcInsert .

This is what I did.

  1. I made sure the MAP only has columns that I want inserted.

  2. 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()]);`
    
  3. 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.

like image 44
MAYOBYO HASSAN Avatar answered Sep 21 '22 23:09

MAYOBYO HASSAN


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

like image 33
Krzysztof Wolny Avatar answered Sep 20 '22 23:09

Krzysztof Wolny


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.

like image 44
Doron Gold Avatar answered Sep 21 '22 23:09

Doron Gold