0

I am getting a problem using hibernate with postgresql

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name = "id")
private Long id;

the problem is the id is generating in 10s eg

10
20
30

How do I make it

1
2
3
Shahzeb
  • 4,745
  • 4
  • 27
  • 40
Paul
  • 1,375
  • 4
  • 16
  • 29

3 Answers3

0

See hibernate allocationSize

Also see this - hibernate oracle sequence produces large gap

Community
  • 1
  • 1
Trever Shick
  • 1,734
  • 16
  • 17
0

As it has been pointed out, you shall use another annotation @javax.persistence.SequenceGenerator Here is how I did

@Id
@javax.persistence.SequenceGenerator(name = "order_id_sequence", sequenceName = "order_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_id_sequence")
@Column(name = "id")
Toto
  • 89,455
  • 62
  • 89
  • 125
vic
  • 2,548
  • 9
  • 44
  • 74
0

Just so you know, you won't be able to rely on values from sequences not having gaps, as the database can't roll back the sequence in situations like:

  1. Session A pulls a number, say 1, from the sequence.
  2. Session B pulls a number, 2, from the sequence.
  3. Session A rolls back its transaction.
  4. Session B commits.

So, I would advise you to either not worry about it or use a different method for producing a series of unique numbers without gaps.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62