简体   繁体   English

如何在spring数据JDBC中插入默认值

[英]How do you insert with default value in spring data JDBC

CrudRepository#save doesn't allow you to use default columns. CrudRepository#save不允许您使用默认列。 null-fields of an entity are interpreted as NULL not DEFAULT .实体的空字段被解释为NULL而不是DEFAULT

If I use a custom @Query("INSERT INTO ... DEFAULT ...") , then I'm unable to obtain the ID of the inserted row.如果我使用自定义@Query("INSERT INTO ... DEFAULT ...") ,那么我将无法获得插入行的 ID。

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.虽然@Jay 的回答并非针对 Spring Data JDBC,但在构造函数中将属性设置为其默认值的方法确实适用于 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. AFAIK 并非所有数据库都支持一个插入的多个返回值,因此您可能必须实际重新选择写入数据库的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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