[英]Insert only a record using CrudRepository
When save from CrudRepository
in Spring data is called, it either updates the existing record in the database or creates a new record if the id for that record is null/blank. 当从Spring的
CrudRepository
中保存数据时,它会更新数据库中的现有记录,或者如果该记录的ID为空/空白,则会创建一个新记录。
I have a app which inserts and updates records in a table where primary key is a String
, so not increment, but the value stored in that column is Integer
. 我有一个应用程序,该应用程序在主键为
String
的表中插入和更新记录,而不是增量,但存储在该列中的值为Integer
。 So the app gets the max(last_insert_id(string))
and increments it for the new record and saves it to the database. 因此,应用程序将获取
max(last_insert_id(string))
并将其递增以获取新记录,并将其保存到数据库中。 There is another app which is doing the same process and in some cases, the data is getting overwritten. 还有另一个应用程序正在执行相同的过程,在某些情况下,数据将被覆盖。
So when using CrudRepository
, if I can only insert the record instead of updating it, that should help in resolving the issue. 因此,在使用
CrudRepository
,如果我只能插入记录而不是对其进行更新,那将有助于解决问题。 How can i create only a new record using CrudRepository
, is there any built in support for it ? 如何使用
CrudRepository
仅创建新记录,对此有内置支持吗?
Better use a custom sequence generator for your scenario. 最好为您的方案使用自定义序列生成器。
https://vladmihalcea.com/how-to-implement-a-custom-string-based-sequence-identifier-generator-with-hibernate/ https://vladmihalcea.com/how-to-implement-a-custom-string-based-sequence-identifier-generator-with-hibernate/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.