patrykos91 patrykos91 - 1 year ago 87
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:

@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
and trying to use that global sequence for id generation.

When I switch to
, it uses
and it works fine.

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

Answer Source

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