Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

How do Oracle sequences work with JPA's @GeneratedValue(strategy = SEQUENCE) and allocationSize? What happens if the sequence INCREMENT BY does not match allocationSize?

With @GeneratedValue(strategy = SEQUENCE), Hibernate calls SELECT seq.NEXTVAL FROM dual and, with its default pooled optimizer, treats each value as the end of a block of allocationSize ids (50 by default) that it hands out from memory. This only works when the sequence's INCREMENT BY equals allocationSize. If they differ, Hibernate either refuses to start (its default mismatch check) or, if that check is turned off, two application instances can hand out overlapping ids and inserts fail with ORA-00001 unique constraint violations.

Why this question is asked

Almost every JPA application on Oracle generates its primary keys from a sequence, and most of them never think about it until a deploy fails or a batch job starts throwing unique constraint errors. Interviewers ask this because it tests whether we understand the contract between the ORM and the database rather than just the annotation. In this answer, we will recap what an Oracle sequence is, follow how Hibernate turns one NEXTVAL into 50 ids, see what breaks when INCREMENT BY and allocationSize disagree, and then look at caching, gaps, identity columns and RAC.

1A Quick Recap of Oracle Sequences

A sequence is a database object that hands out unique numbers. Each call to NEXTVAL returns the next number and moves the sequence forward by its INCREMENT BY value. Sequences are not part of any transaction: if our transaction rolls back, the number we took is not returned. That is what makes them fast, because two sessions never wait for each other to commit.

SQL
CREATE SEQUENCE orders_seq START WITH 1 INCREMENT BY 50 CACHE 20;
SELECT orders_seq.NEXTVAL FROM dual; -- 1
SELECT orders_seq.NEXTVAL FROM dual; -- 51
SELECT orders_seq.NEXTVAL FROM dual; -- 101

2How Hibernate Turns One NEXTVAL Into 50 Ids

On the Java side we map the entity to that sequence and tell Hibernate the allocation size. The value 50 is also the JPA default, so leaving it out gives the same result.

Java
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "orders_gen")
@SequenceGenerator(name = "orders_gen", sequenceName = "orders_seq", allocationSize = 50)
private Long id;
}
// Hibernate runs: select orders_seq.nextval from dual -> 101
// With the pooled optimizer, 101 is the top of a block: ids 52 to 101 are now ours
em.persist(new Order()); // id 52, no database call
em.persist(new Order()); // id 53, no database call
// ... the 51st persist triggers the next NEXTVAL

The pooled optimizer, which is Hibernate's default when allocationSize is greater than 1, reads one value and treats it as the upper end of a block of allocationSize ids. The ids in between are handed out from memory. So 50 inserts cost one sequence call instead of 50, and the next instance that calls NEXTVAL gets 151 and uses 102 to 151. The exact handling of the very first block differs slightly between optimizers and versions, so check your version if the first ids matter to you.

Notice that this only works because the database moves 50 per call. Each instance owns the 50 numbers below the value it read, and no other instance can read a value inside that range.

3What Happens When INCREMENT BY Does Not Match

Now imagine a DBA created the sequence with the default INCREMENT BY 1, while the entity still says allocationSize = 50. Two application instances start.

Java
-- sequence created as INCREMENT BY 1, entity says allocationSize = 50
-- instance A: nextval -> 101, uses ids 52..101
-- instance B: nextval -> 102, uses ids 53..102
-- A inserts id 60, B inserts id 60
-- ORA-00001: unique constraint (APP.ORDERS_PK) violated
Same NEXTVAL calls, two configurations
CallINCREMENT BY 50INCREMENT BY 1
Instance A reads101 -> uses 52..101101 -> uses 52..101
Instance B reads151 -> uses 102..151102 -> uses 53..102
ResultNo overlapids 53..101 claimed by both, ORA-00001

Each instance believes it owns a block of 50, but the blocks overlap almost entirely. The failures appear only under concurrency, which is why they often show up after a scale-out rather than in testing.

Modern Hibernate protects us from this. At startup it reads the sequence's real increment from the database metadata and compares it with allocationSize. The setting hibernate.id.sequence.increment_size_mismatch_strategy controls what happens; its default is to throw an exception and stop the application, and it can also log, fix the allocation size to match the database, or do nothing (check your version for the exact default). The fix is always to make the two agree, either by altering the sequence to INCREMENT BY 50 or by setting allocationSize to the sequence's increment.

4Sequence CACHE and Why Gaps Are Normal

Separately from Hibernate, Oracle has its own CACHE setting on the sequence (20 values by default). The database keeps that many future values in memory so NEXTVAL rarely has to update the data dictionary. If the instance restarts or the values age out of the shared pool, the unused cached values are lost.

Put those together and gaps are guaranteed: Oracle loses cached values, Hibernate loses the rest of a block when an application restarts, and rolled-back transactions keep their numbers. So we never rely on ids being gap-free or strictly in insert order. If the business needs a gap-free invoice number, that is a different design, usually a counter row updated inside the transaction, with the locking cost that implies.

5Identity Columns and Why SEQUENCE Still Wins for Batching

Since Oracle 12c we can declare id NUMBER GENERATED AS IDENTITY. Oracle creates an internal sequence for the column, so the database side is the same machinery. The difference is on the Hibernate side. With GenerationType.IDENTITY, the id is only known after the row is inserted, so Hibernate must execute each INSERT immediately to learn the id, and it cannot group inserts into JDBC batches.

With SEQUENCE, Hibernate knows the id before the insert, so it can delay the INSERTs to flush time and send them in batches when hibernate.jdbc.batch_size is set. For bulk inserts this is often the difference between minutes and seconds, which is why SEQUENCE with a sensible allocationSize is the usual recommendation on Oracle.

6Sequences on Oracle RAC

On RAC, several database instances share one database, and each instance keeps its own sequence cache. With the default NOORDER, instance 1 might hand out 1 to 20 while instance 2 hands out 21 to 40, so values are unique but not in time order across the cluster. ORDER forces strict ordering, which means the instances must coordinate on every NEXTVAL, and that becomes a contention point. A small CACHE has a similar effect, because instances have to refresh their ranges often. For a busy table on RAC we normally want NOORDER with a large cache, such as CACHE 1000, plus Hibernate's allocationSize to cut the number of calls further.

7Follow-up questions

Why is allocationSize = 1 a bad idea for bulk inserts?

Every new entity then needs its own NEXTVAL round trip before the insert, so inserting 100,000 rows means 100,000 extra calls. It is correct and has no mismatch risk, but it removes the main performance benefit of the sequence strategy.

What happens if a legacy system also inserts rows using the same sequence with NEXTVAL directly?

With INCREMENT BY 50 and the pooled optimizer this is safe, because the legacy system simply uses the value it gets and Hibernate uses the 50 values below the value it gets, and those ranges never overlap. With older optimizers such as hilo, where Hibernate multiplied the value, other writers could collide, which is one reason pooled became the default.

How do you change allocationSize on a live system?

Change the sequence and the mapping together in one release. First move the sequence past every id in use, for example by recreating it starting above the current maximum id, then set INCREMENT BY to the new size and deploy the matching allocationSize. Running old and new application versions side by side with different sizes recreates the overlap problem, so the switch needs a short window or a sequence that starts in a fresh range.

8Conclusion

In this answer, we saw that Hibernate's pooled optimizer reads one NEXTVAL and hands out the allocationSize ids below it from memory, 50 by default. That only works when the sequence's INCREMENT BY equals allocationSize; otherwise instances claim overlapping blocks and inserts fail with ORA-00001, which is why Hibernate checks the increment at startup. We also saw that gaps are normal because of Oracle's sequence cache, restarts and rollbacks, that identity columns use an internal sequence but stop Hibernate from batching inserts, and that on RAC NOORDER with a large cache keeps sequences from becoming a bottleneck.

What to say in the interviewAbout 90 seconds aloud

Open with

With GenerationType.SEQUENCE, Hibernate runs select seq.nextval from dual, but it does not call it for every row. With the default pooled optimizer and an allocationSize of 50, each value it reads is the top of a block of 50 ids, and it hands those out from memory, so 50 inserts cost one sequence call.

Then, if they want more

The contract is that the sequence's INCREMENT BY must equal allocationSize. If the DBA created the sequence with INCREMENT BY 1, two instances reading 101 and 102 would both think they own most of the same block, and under load we would get ORA-00001 unique constraint violations.

Current Hibernate checks the increment against the database at startup and by default fails fast, so the fix is to align the two. I would also mention that gaps are normal: Oracle caches sequence values in memory and loses them on restart, Hibernate loses unused blocks, and rolled-back transactions keep their numbers, so ids should never be treated as gap-free.

Identity columns from 12c use an internal sequence too, but Hibernate cannot batch inserts with IDENTITY because it needs the id after each insert, so SEQUENCE is better for bulk work. On RAC I would use NOORDER and a large CACHE, because ORDER or a tiny cache forces the instances to coordinate on every call.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do Oracle sequences work with JPA's @GeneratedValue(strategy = SEQUENCE) and...": https://scrivio-interview-library.netlify.app/databases/oracle-and-sql-server/e58235778f/