英文:
How to implement auto-increment field other than id field?
问题
我想实现一个自增字段,除了id字段外,它从1开始按顺序递增。
代码示例:
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id; // 主键id
@Column(name = "request_number", nullable = false, unique = true, updatable = false, insertable = false)
@GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
private Long requestNumber; // 就是这个字段
因此,这里的 requestNumber
应该在每次创建对象时自动增加。并且应该按顺序递增。
示例:第一个条目的 requestNumber
将从 1
开始,下一个 requestNumber
将分配为 2
,依此类推。
英文:
I want to implement one auto increment field other than id field that starts with 1 and increase by 1 sequentially.
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. 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...
I know it is possible via java code but I am looking for JPA provide such flexibility.
答案1
得分: 1
@GeneratedValue
仅用于简单的主键,根据javadoc的说明:
> GeneratedValue注解可以应用于实体或映射的超类的主键属性或字段,与Id注解一起使用。生成值注解的使用仅需要支持简单的主键。不支持对派生的主键使用生成值注解。
如果您想在JPA中实现,可以定义一个类似于@PrePersist
的方法,如下所示:
@PrePersist
void doPrePersist() {
// 使用EntityManager创建本地查询
// 从序列中读取下一个值
// 设置字段值
}
另一种选择是将数据库列定义为IDENTITY
,但这会在JPA之外自动增加。例如,实体字段在插入时不会被插入,且在实体持久化操作期间看不到该值。
请注意,SQL Server和大多数数据库都不能保证序列中不会有间隙。当增加序列的事务被回滚时,序列的值不会回滚,因此您可能会得到这样的序列:1,2,4,5,6,10,...
英文:
@GeneratedValue
is used only for simple primary keys, as per the 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. The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of the GeneratedValue annotation is not supported for derived primary keys.
If you want to do it in JPA you can define a @PrePersist
method like:
@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 e.g. the entity field won't be insertable and value won't be seen during entity persist operation.
Please note that SQL Server, and most databases, doesn't guarantee that there won't be gaps in the sequence. 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, ...
答案2
得分: 0
你必须在你的类(或字段)上声明一个 @SequenceGenerator
注解:
@SequenceGenerator(sequenceName = "MY_DB_SEQUENCE", name = "sequence")
public class MyClass {
// 保持不变
}
注意,@GeneratedValue
上的 generator = "sequence"
指向了具有 name = "sequence"
的 @SequenceGenerator
。
英文:
You have to declare a @SequenceGenerator
annotation on your class (or field):
@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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论