Issue: Hibernate uses generator name rather than sequence name

I am using Spring boot 2.5.0 + Hibernate 5.4.3 + Postgres 13 + gradle
I have 2 entity class.

public class ABC implement Serializable {

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_generator")
@SequenceGenerator(name = "pk_generator", sequenceName = "default_sequence")
private Long abcId;


And In another package

public class DEF implement Serializable {

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_generator")
private Long defId;

When I build the Jar and insert in ABC it runs fine.

        nextval ('default_sequence')

While when I insert something on DEF then it runs.

        nextval ('pk_generator')

Which result in SQL exception.

Just to note both of them are in the different package

I don’t think the generators are “global”, so you might have to redefine it for every entity, ideally with a different name.

But as per JPA doc it claims to be global

So is it bug in Hibernate which overwrites that behavior somehow?

I have this 7 year old stack overflow thread which claims the same?

So is it still true?

In the past, Hibernate scoped the sequences per entity and then came JPA saying that this should be global, so in order to allow both, a setting was introduced to control this. You can enable global scoping by setting hibernate.jpa.compliance.global_id_generators to true. Also see the documentation about the details: Hibernate ORM 5.5.3.Final User Guide

I see. But do you know or may be help us on how to set that property in spring boot application.yml or some another way. Since we don’t use hibernate.cfg.xml

You would need to ask the Spring community on how to set this, but usually you just add that property to application.yml like this:

      hibernate.jpa.compliance.global_id_generators: true

Thanks @beikov for you help. This setting worked for us.

By default id generators are local in keeping with Hibernate’s legacy and much more sane behavior.

JPA does require these to be globally scoped. If you wish the JPA behavior, enable JPA compliance (hibernate.jpa.compliance.global_id_generators). Or use JPA bootstrapping which then enables this by default.