patrykos91 patrykos91 - 3 months ago 23
Java Question

Oracle JPA GenerationType.AUTO with generator asks for "hibernate_sequence"

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)?

Answer

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