简体   繁体   中英

How do you insert with default value in spring data JDBC

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM