java - JPA,GeneratedValue 自动递增 50

标签 java jpa entity

我正在使用 JPA,当我将数据插入数据库时​​,我的 ID 会自动增加 50。 我正在使用 persistence.GeneratedValue 来自动增加它:

这是我的模型/实体类的样子(要插入的实体):

..imports
@Entity
public class Example extends Identifiable  {
   ..
}

可识别:

..imports
@MappedSuperclass
public abstract class Identifiable {
    @Id @GeneratedValue protected long id;
    ..
}

数据库:

#    ID    NAME    ...
1    701   a
2    751   b
3    801   c

有人知道问题出在哪里吗?

最佳答案

GeneratedValue.strategy 的默认值为 GenerationType.AUTO。 JPA 2 规范中清楚地说明了这意味着什么:

The AUTO value indicates that the persistence provider should pick an appropriate strategy for the particular database. The AUTO generation strategy may expect a database resource to exist, or it may attempt to create one. A vendor may provide documentation on how to create such resources in the event that it does not support schema generation or cannot create the schema resource at runtime.

对于TableGeneratorSequenceGenerator allocationSize 的默认值为 50。这意味着保留 50 个值的 block 。在关闭时,保留值将消失。如果应用程序在仅使用一个后关闭,则从 2 到 50 的值将消失,下一次将保留 51 到 100。如果需要对标识符生成进行更多控制,则应使用 auto 以外的策略。例如,可以按如下方式完成:

@TableGenerator(
  name = "yourTableGenerator"
  allocationSize = 1,
  initialValue = 1)
@Id 
@GeneratedValue(
  strategy=GenerationType.TABLE, 
  generator="yourTableGenerator")

or:

@SequenceGenerator(name="yourSequenceGenerator", allocationSize=1)
@Id 
@GeneratedValue(strategy=GenerationType.SEQUENCE, 
                generator="yourSequenceGenerator")

关于java - JPA,GeneratedValue 自动递增 50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578985/

相关文章:

java - 如何限制子类覆盖/实现父类(super class)方法

java - Spring JPA/Hibernate org.hibernate.AssertionFailure : null id in Entity (don't flush the Session after an exception occurs)

php - 如何从 Symfony2 上的另一个 Bundle 加载实体

java - 实体类和记录锁定

java - XmlBeans 与 Digester 哪个更擅长处理巨大的 xml 文件?

java - RxJava 中的过滤器

Java 规范 - "valid"优化的规则是什么

java - 如何覆盖 FetchType.EAGER 以在运行时变得懒惰

java - 使用数组进行 JPA 查找

c# - 永久禁用 EF 中的 Configuration.ProxyCreationEnabled?