Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence generator : JAVA/JPA/SPRING

Question: Is it possible to generate sequence number not using select someSequence from dual; ?

Problem : @GeneratedValue and @SequenceGenerator by default is using select someSequence from dual; to get nextval for my id. But my user doesn't have such rights to select from the dual table so I can use only id = sequence.nextval, but I don't know how to use this in Entity class. Or how should I pass id value to Entity constructor like sequence.nextval?

Usage: Oracle DB, CrudRepository, save() method.

like image 396
D1' Avatar asked Oct 23 '25 05:10

D1'


2 Answers

You need to first create the sequence in Oracle:

CREATE SEQUENCE USER_SEQUENCE START WITH 1 INCREMENT BY 10;

Then annotate your class to look like this:

@Entity
@SequenceGenerator(name="USER_SEQUENCE_GENERATOR", sequenceName="USER_SEQUENCE", initialValue=1, allocationSize=10)
public class User {
    @Id
    @Column(name="USER_ID")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="USER_SEQUENCE_GENERATOR")
    private Long userId;
}
like image 146
sechanakira Avatar answered Oct 24 '25 19:10

sechanakira


I think you would need to write yout own strategy and use the GenericGenerator annotation. This answer might be helpful for you: link

like image 22
KaPrimov Avatar answered Oct 24 '25 19:10

KaPrimov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!