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;
We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

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.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 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. – We are Borg Mar 31 '17 at 08:39
  • No, it won't be okay actually. See http://stackoverflow.com/questions/2595124/java-jpa-generators-sequencegenerator and http://stackoverflow.com/questions/5346147/hibernate-oracle-sequence-produces-large-gap 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. – Kayaman Mar 31 '17 at 08:44