简体   繁体   English

如何在 JPA 中定义用于 Id (promary) 的自定义序列生成器?

[英]How to define custom sequence generator used for Id (promary) in JPA?

My application is using Jakarta EE JPA for persisting data to Database.我的应用程序使用 Jakarta EE JPA 将数据保存到数据库。 The application has to generate Custom(encoded) sequence for performance reasons.出于性能原因,应用程序必须生成自定义(编码)序列。 By default JPA seems to generate Ids for an entity using some sequence.默认情况下,JPA 似乎使用某些序列为实体生成 ID。

How to override default sequence generator with customer sequence generator coded in Java?如何使用编码为 Java 的客户序列生成器覆盖默认序列生成器?

This is how you go with the custom sequence:这就是您使用自定义序列 go 的方式:

@Id
@SequenceGenerator(name = "pet_seq", 
        sequenceName = "pet_sequence", 
        initialValue = 1, allocationSize = 20)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pet_seq")
@Column(name = "id", nullable = false)
private Long id;

In this case it will use pet_sequence instead of the default one.在这种情况下,它将使用pet_sequence而不是默认的。 Also you can read this article for a better understanding of this subject.您也可以阅读本文以更好地了解该主题。

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

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