0
  1. in postgres I create sequence: user_details_userid_seq
  2. now I want to use it in my class
public class UserDetails {
    @Id
    private int userId;

I want what userId would read and generate next value of user_details_userid_seq (which is defined in databas). How to do this?

2
  • 1
    @GeneratedValue(strategy = SEQUENCE, generator = "user_details_generator") @SequenceGenerator(name = "user_details_generator", sequenceName="user_details_userid_seq", allocationSize = <your_sequence_increment>)
    – crizzis
    Nov 1, 2020 at 10:13
  • 1
    Just keep in mind that allocationSize = 1 might not be the most performant solution. However, if other apps can insert data into your table, it is also the least confusing
    – crizzis
    Nov 1, 2020 at 10:18

1 Answer 1

1

Thanks crizzis for keywords to search, but solution:

@GeneratedValue(generator="my_seq_gen")
@SequenceGenerator(name="my_seq_gen", sequenceName="user_details_userid_seq", allocationSize = 1)

where the parameter allocationSize = 1 defines your sequence-increment as 1.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.