1

I have a sequence created using flyway in postgres which should start from 10000.

I want to get the next value of the sequence using JPA and not a native query , since i have different db platforms being run at different cloud providers.

I'm not able to find a JPA query to get the next value of a sequence, please redirect me to the right page if i am missing something already ..

Thanks for any help in that area though already!

P.S : I found this link which helps me doing the same with native query. postgresql sequence nextval in schema

mohneesh_d
  • 45
  • 7

1 Answers1

0

I don't think this is possible in a direct way.

JPA doesn't know about sequences. Only the implementation knows about those and utilizes them to create ids.

I see the following options to get it to work anyway:

  1. create a view in the database with a single row and a single column containing the next value. You can query that with native SQL which should be the same for all databases since it is a trivial select.
  2. Create a dummy entity using the sequence for id generation, save a new instance and let JPA populate the id. A horrible workaround but pure JPA.
  3. Bite the bullet and create a simple class that provides the correct native SQL statement to use for the current environment and execute it via JdbcTemplate.
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348