Skip to main content
2 of 3
edited tags; edited tags
tadman
  • 212.4k
  • 23
  • 237
  • 266

How to define shared sequence generator in Hibernate / JPA?

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?

gavenkoa
  • 49.7k
  • 28
  • 274
  • 341