0

I have a standard JPA @SequenceGenerator annotated entity:

@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(generator = "mySeq", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "mySeq", sequenceName = "MY_SEQ", allocationSize = 10)  
private long id;

with the named Oracle Sequence defined as:

CREATE SEQUENCE MY_SEQ
 START WITH 55554444
 INCREMENT BY 1
 NOORDER NOCYCLE;

When I persist through a standard Java, Spring Data Service, the first persisted, generated ID value is:

555544440

In other words, it's * 10 what I had defined as a starting value. Is this expected behaviour?

When I query:

select last_number from dba_sequences

though, the returned value is still of the 55554444 range.

Running an equivalent insert directly on the DB

INSERT INTO MY_TABLE (ID) VALUES (MY_SEQ.nextVal)

the ID value is generated and persisted as I would expect; i.e 55554444, 55554445, 55554446, etc. (and also correlates the last_number in dba_sequences)

Whats going on! How and why is the JPA persistence * 10 my sequence IDs!?

Stumped, any help appreciated!

thanks, Damien

Damo
  • 1,449
  • 3
  • 16
  • 29
  • Not a direct answer to your question, but please note that Oracle sequences are not guaranteed to produce a contiguous sequence of values. There will be gaps (possibly large ones) due to caching etc. – Mick Mnemonic Dec 08 '16 at 19:17
  • Okay, so the allocationSize is doing the X - http://stackoverflow.com/questions/12745751/hibernate-sequencegenerator-and-allocationsize – Damo Dec 08 '16 at 19:29

1 Answers1

0

Hibernate, @SequenceGenerator and allocationSize answered this.

That is, if the JPA defined allocationSize differs from the oracle sequence increment size, bad things happen.

For me: JPA allocationSize = 10 DB sequence increment = 1

Java persistence was then multiplying the oracle sequence * 10

Community
  • 1
  • 1
Damo
  • 1,449
  • 3
  • 16
  • 29