I have an entity simply called Order. The strategy I’m using now for the ID is:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ORDER_ID")
private Long id;
Now, hardcoding another startvalue seems easy:
@Entity
@SequenceGenerator(name = "SequenceIdGenerator",
sequenceName = "SEQ_ID_GEN", initialValue = 50,
allocationSize = 20)
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SequenceIdGenerator")
@Column(name = "ORDER_ID")
private Long id;
But that’s hardcoded to 50. Can I set that value dynamically? Best would be if I could have it stored in the database or in a properties file?
Or is it not best practice to set id dynamically?
Usually, but not always, the easiest way is to create a sequence in your database and to use that sequence. I hope there is not much mistakes in following example: