0

I am working on a Spring-MVC project in which we are using Hibernate as our ORM and PostgreSQL as the database. We have a Students class, whose ID we are using in one of the PDF form generated, and for business reasons we require that the number be incremented by 1 always. I checked that the allocation-size parameter can be changed for Hibernate, but does it also effect the underlying sequence in PostgreSQL or do I have to create a new one?

If the underlying sequence is not altered by Hibernate, how can I change the sequence so that it's auto-incremented by 1 in PostgreSQL. Thank you.

Code :

  @Id
    @Column(name="studentid")
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "student_gen")
    @SequenceGenerator(name = "student_gen",sequenceName = "student_seq",allocationSize = 1)
    private int studentid;

1 Answer 1

2

No, the sequence in Postgres won't be changed just by modifying the Hibernate annotation.

You can use ALTER SEQUENCE student_seq INCREMENT BY 1 to modify the sequence.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. If I don't change the allocation size on Model and just give the above command, is that okay? As it will take us a new deployment. Thank you.
No, it won't be okay actually. See stackoverflow.com/questions/2595124/… and stackoverflow.com/questions/5346147/… for more info. Basically the generator works with the database sequence, and unless the allocationSize is the same as INCREMENT BY, the values generated can become really odd.

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.