简体   繁体   English

如何实现id字段以外的自增字段?

[英]How to implement auto-increment field other than id field?

I want to implement one auto increment field other than id field that starts with 1 and increase by 1 sequentially.我想实现一个自动递增字段,而不是从 1 开始并按顺序增加 1 的 id 字段。

Code Sample :代码示例:

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id; //Id primary key

@Column(name = "request_number", nullable = false, unique = true, updatable = false, insertable = false)
@GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
private Long requestNumber; //Talking about this

So, here requestNumber should increase automatically every time when ever object create.因此,每次创建对象时,这里requestNumber都应该自动增加。 And that should increase sequentially.这应该会依次增加。

Example : First entry's requestNumber will start with 1 and next requestNumber will be assign with 2 and so on...示例:第一个条目的requestNumber将从1开始,下一个requestNumber将被分配为2 so on...

I know it is possible via java code but I am looking for JPA provide such flexibility.我知道通过 java 代码是可能的,但我正在寻找 JPA 提供这种灵活性。

@GeneratedValue is used only for simple primary keys, as per the javadoc: @GeneratedValue仅用于简单的主键,根据 javadoc:

The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. GeneratedValue 注释可以与 Id 注释一起应用于实体或映射超类的主键属性或字段。 The use of the GeneratedValue annotation is only required to be supported for simple primary keys. GeneratedValue 注解的使用只需要支持简单的主键。 Use of the GeneratedValue annotation is not supported for derived primary keys.派生主键不支持使用 GeneratedValue 注释。

If you want to do it in JPA you can define a @PrePersist method like:如果你想在 JPA 中做到这一点,你可以定义一个@PrePersist方法,如:

@PrePersist
void doPrePersist() {
    // Use EntityManager to create a native query
    // read next value from a sequence
    // set the field value
}

Another option would be to define the database column as IDENTITY but this will take care of auto incrementing outside of JPA eg the entity field won't be insertable and value won't be seen during entity persist operation.另一种选择是将数据库列定义为IDENTITY但这将处理 JPA 之外的自动递增,例如实体字段将不可插入,并且在实体持久操作期间将看不到值。

Please note that SQL Server, and most databases, doesn't guarantee that there won't be gaps in the sequence.请注意,SQL Server 和大多数数据库不保证序列中不会出现间隙。 When a transaction that increments sequence is rolled back the value of the sequence is not, so you can end up with: 1, 2, 4, 5, 6, 10, ...当一个增加序列的事务被回滚时,序列的值不是,所以你可以得到:1, 2, 4, 5, 6, 10, ...

You have to declare a @SequenceGenerator annotation on your class (or field):你必须在你的类(或字段)上声明一个@SequenceGenerator注释:

@SequenceGenerator(sequenceName = "MY_DB_SEQUENCE", name = "sequence")
public class MyClass {
    // keep as is
}

Note that the generator = "sequence" on @GeneratedValue points to the @SequenceGenerator with the name = "sequence"请注意, generator = "sequence"@GeneratedValue指向@SequenceGeneratorname = "sequence"

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

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