2

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

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.