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字段作为主键,所以每次持久化对象时,id的值都从序列“myClass_pk_seq”中检索,代码如下所示。
@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?
我需要在“code”属性中创建类似于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?
我只想要一个方法在与“code”字段关联的序列上调用nextval,并返回该值。使用注释在JPA中执行此操作的最佳方法是什么?
Thanks.
2 个解决方案
#1
3
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?
我只想要一个方法在与“code”字段关联的序列上调用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?
实际上,您没有提及有关代码唯一性的任何内容(并且JPA注释未显示它必须是唯一的)。你为什么不返回一个随机的int?
#2
0
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字段以外定义的序列的主题的其他问题/答案。您可以使用一个字段(Long id类型)创建虚假实体。将其连接到您在DB中定义的序列。然后为该实体创建一个CrudRepository实现,并使用您定义的假实体对象的空实例调用其save()方法。 Hibernate将为您运行“从双重选择YOUR_SEQ.NEXTVAL”查询。
#1
3
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?
我只想要一个方法在与“code”字段关联的序列上调用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?
实际上,您没有提及有关代码唯一性的任何内容(并且JPA注释未显示它必须是唯一的)。你为什么不返回一个随机的int?
#2
0
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字段以外定义的序列的主题的其他问题/答案。您可以使用一个字段(Long id类型)创建虚假实体。将其连接到您在DB中定义的序列。然后为该实体创建一个CrudRepository实现,并使用您定义的假实体对象的空实例调用其save()方法。 Hibernate将为您运行“从双重选择YOUR_SEQ.NEXTVAL”查询。