Java 在 jpa 中调用序列的下一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3067986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:11:05  来源:igfitidea点击:

Calling next value of a sequence in jpa

javahibernatejpasequencenextval

提问by Javi

I have a class mapped as an Entity to persist it in a database. I have an id field as the primary key so every time the object is persisted the value of the id is retrieved from the sequence "myClass_pk_seq", a code like the following one.

我有一个映射为实体的类以将其保存在数据库中。我有一个 id 字段作为主键,所以每次保存对象时,都会从序列“myClass_pk_seq”中检索 id 的值,代码如下所示。

@Entity
@Table(name="myObjects")
public class MyClass {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
    @SequenceGenerator(name="sequence", sequenceName="myClass_pk_seq", allocationSize=1)
    @Column(name="myClassId")
    private Integer id;

    private Integer code;


    ...
}

I need to make in "code" attribute something similar to id. I need to have a sequence so I can assign to code the next value of the sequence (to reserve that value in case the user wants to reserve it but without persisting data). I mean the user will see the field, if he doesn't know what to enter he can push a button and receieve in the screen the next possible value (and he can or not accept it). How can I get the next value of a sequence defined in JPA (and increment its value) without persisting the data for a non primary key field?

我需要在“代码”属性中添加类似于 id 的内容。我需要一个序列,以便我可以分配给序列的下一个值进行编码(以保留该值,以防用户想要保留它但没有持久化数据)。我的意思是用户将看到该字段,如果他不知道要输入什么,他可以按下按钮并在屏幕上接收下一个可能的值(他可以接受或不接受)。如何在不保留非主键字段的数据的情况下获取 JPA 中定义的序列的下一个值(并增加其值)?

I just want to have a method which call nextval on a sequence associated with "code" field, and returns the value. What's the best way to do it in JPA with annotations?

我只想有一个方法在与“代码”字段关联的序列上调用 nextval 并返回值。使用注释在 JPA 中执行此操作的最佳方法是什么?

Thanks.

谢谢。

采纳答案by Pascal Thivent

I just want to have a method which call nextval on a sequence associated with "code" field, and returns the value. What's the best way to do it in JPA with annotations?

我只想有一个方法在与“代码”字段关联的序列上调用 nextval 并返回值。使用注释在 JPA 中执行此操作的最佳方法是什么?

  • Use native SQL to get the next sequence value when the user pushes the button. Either create the sequence manually or use a "fake entity" to have JPA create it for you.
  • If you don't want to use native SQL, insert an entity relying on the sequence and gets its id.
  • 当用户按下按钮时,使用原生 SQL 获取下一个序列值。手动创建序列或使用“假实体”让 JPA 为您创建它。
  • 如果不想使用原生 SQL,则插入依赖于序列的实体并获取其 id。

Both solutions sounds a bit ugly. Maybe you could simply use a random generator like a UUID generator.

这两种解决方案听起来都有些难看。也许您可以简单地使用像 UUID 生成器这样的随机生成器。

Actually, you didn't mention anything about the uniqueness of the code(and the JPA annotations don't show it must be unique). Why don't you return a random int?

实际上,您没有提到任何有关code( 并且 JPA 注释没有表明它必须是唯一的) 的唯一性。你为什么不返回一个随机整数?

回答by razvanone

See another question/answers on the subject of using sequence defined elsewhere than id fields. You can create a fake entity with one field (of type Long id). Connect it to the sequence you defined in the DB. Then create a CrudRepository implementation for that entity and call its save() method with an empty instance of the fake entity object you defined. Hibernate will run for you a "select YOUR_SEQ.NEXTVAL from dual" query.

请参阅有关使用在 id fields 以外的其他地方定义的序列的主题的另一个问题/答案。您可以使用一个字段(Long id 类型)创建一个假实体。将其连接到您在 DB 中定义的序列。然后为该实体创建一个 CrudRepository 实现,并使用您定义的假实体对象的空实例调用其 save() 方法。Hibernate 将为您运行“select YOUR_SEQ.NEXTVAL from dual”查询。