简体   繁体   English

春季数据-JPA,缺少ID

[英]Spring data - JPA, missing IDs

We are using a spring data project, we have a mysql database. 我们正在使用一个spring数据项目,我们有一个mysql数据库。 Lately we have noticed the following on the USER table: 最近,我们在USER表上注意到以下内容:

id  |   name |  pass | email
=====================================
1      John      123     john@gmail.com

2      Joe      456       joe@gmail.com

3      .....

4   ....

6 ...

Well apparently id=5 is missing from our database. 很显然,我们的数据库中缺少id = 5。

  1. What could be the reason for that? 可能是什么原因呢? Bear in mind we are using @transactional services and dao. 请记住,我们正在使用@transactional服务和dao。

  2. Can spring data create new entries with these skipped ids? Spring数据可以使用这些跳过的ID创建新条目吗? (configuration maybe?) (可能是配置?)

The id get 'reserved' when you issue an insert statement, but the insertion is performed until the transaction is commited; 当您发出insert语句时,标识将被“保留”,但是插入操作将一直执行到提交事务为止。 if an exception is thrown before that, the transaction gets rolled back, the insertion is not performed, so the reserved id is not used. 如果在此之前引发异常,则事务将回滚,不执行插入操作,因此不使用保留的ID。 All of this is assuming you're using auto-increments. 所有这些都是假设您正在使用自动增量。

There might be other reasons, but this one in particular has happened to me a on ocassion, although is not really an issue to me. 可能还有其他原因,但是这对我来说尤其是偶然的,尽管这对我而言并不是真正的问题。

Edit: this has happened to me on Postgres but I think it also happened on mysql; 编辑:这在Postgres上发生在我身上,但我认为在mysql上也发生过; it's been a while since I last used it. 自上次使用以来已经有一段时间了。

Also, you can check your JPA provider's the documentation on @GeneratedValues 另外,您可以在@GeneratedValues上查看JPA提供程序的文档。

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

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