0
votes

I'm trying to use a defined sequence generation in Oracle JPA, along with GenerationType.AUTO like this:

@Id
@SequenceGenerator(name = "MY_GEN_NAME", sequenceName = "MY_SQ_NAME")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "MY_GEN_NAME")
@Column(name = "ID", nullable = false)
private Long id;

After running the hibernate validation it throws an error:

Schema validation: missing sequence [hibernate_sequence]

I guess, it is ignoring my MY_GEN_NAME and trying to use that global sequence for id generation.

When I switch to GenerationType.SEQUENCE, it uses SequenceHiLoGenerator and it works fine.

Why is that happening, and is it possible to make the GenerationType.AUTO work with given sequence for Oracle (possibility to switch to other db)?

2
I think oracle does not have this feature. There is no "really" auto increment functionality. You have to use a sequence. - Patrick
Wait. I think You didn't understand my question correctly. I do not want to use "auto increment". I want to use my own defined sequences along with GenerationType.AUTO. Let's say I have read only Oracle DB where sequences are created, but in the implementation I have to be ready to switch for MySQL (thats why AUTO, and not SEQUENCE). However when I'm using AUTO - even when I tell JPA what sequence to use, it still looks for or tries to create that global hibernate_sequence. - patrykos91

2 Answers

1
votes

AUTO says to the JPA provider, "choose what you want", and it will not use the "generator" attribute in that case.

If you want to use a SEQUENCE then set the strategy to SEQUENCE! That way it will use the sequence definition you have defined

1
votes

Though hibernate-sequence-on-oracle-generatedvaluestrategy-generationtype-auto suggests that this will work, it appears that it is broken for some versions of Hibernate, see HHH-10656.

Edit: Broken in 5.2.0, unbroken in 5.2.8.