How to define shared sequence generator in Hibernate / JPA?

Viewed 2793

We import data to DB by external process and also allow to modify/add data inside app. To avoid UUIDs and simplify code single Oracle DB sequence HIBERNATE_SEQUENCE is used in external process and I need to use it for affected entities.

My naive definitions on each entity are failed:

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HIBERNATE_SEQUENCE")
@SequenceGenerator(name = "HIBERNATE_SEQUENCE", sequenceName = "HIBERNATE_SEQUENCE")

with:

HibernateException: Multiple references to database sequence [hibernate_sequence] were encountered attempting toset conflicting values for 'increment size'.

Defining only on singe entity:

@SequenceGenerator(name = "HIBERNATE_SEQUENCE", sequenceName = "HIBERNATE_SEQUENCE")

(and @GeneratedValue on others) causes:

org.hibernate.AnnotationException: Unknown Id.generator: HIBERNATE_SEQUENCE

How I can use single sequence on different entities?

UPDATE Also I had:

org.hibernate.HibernateException: Multiple references to database sequence [hibernate_sequence] were encountered attempting toset conflicting values for 'increment size'. Found [1] and [50]

at some stage as previously I defined physical sequence as:

create sequence HIBERNATE_SEQUENCE
minvalue 1
maxvalue 9999999999999999999999999999
start with 1
increment by 1;

and default hibernate step is 50...

1 Answers
Related