4

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 Answer 1

1

Looks like stupid solution of using separate Hibernate logical names works:

@Entity public class Cat { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Cat_seq") @SequenceGenerator(name = "Cat_seq", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1) Long id; } @Entity public class Dog { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Dog_seq") @SequenceGenerator(name = "Dog_seq", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1) Long id; } 

SequenceGenerator.sequenceName set physical sequence name which should correctly work as Hibernate can't make ay assumption on exclusive use of physical object in distributed environment which Oracle DB is...

allocationSize = 1 should be provided as default value is 50 which is different fro original physical sequence definition.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.