How do you insert with default value in spring data JDBC

2.5k views Asked by At

CrudRepository#save doesn't allow you to use default columns. null-fields of an entity are interpreted as NULL not DEFAULT.

If I use a custom @Query("INSERT INTO ... DEFAULT ..."), then I'm unable to obtain the ID of the inserted row.

1

There are 1 answers

2
Jens Schauder On

There is currently no build in way of using the default values from the database.

While @Jay's answer isn't aimed at Spring Data JDBC, the approach of setting the attributes to their default value in the constructor does work with Spring Data JDBC.

The alternative would be to implement a custom method which does the insert and retrieves the default values back from the database.

AFAIK not all databases support more then one return value from an insert so you might have to actually reselect the data written to the database.