spring-boothibernatehibernate-5.xsequence-generatorshilo

Spring Boot Hibernate not picking up use-new-id-generator-mappings property


I'm upgrading my project to Spring Boot 2.1.18 that uses Hibernate 5.3.18.

Previously, my entity looked like thus and would use the SequenceHiLoGenerator:

@Entity
@Table(name = "group_link")
@SequenceGenerator(name = "group_link_seq", sequenceName = "group_link_seq")
public class GroupLinkEntity extends BaseObject {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "group_link_seq")
    @Column(name = "group_link_id", unique = true, nullable = false)
    private Long id 
}

Now, by default in Hibernate 5, it uses the SequenceStyleGenerator which causes constraint violations because my increment size is 1 and the default allocationSize is 50.

The suggested thing to do to maintain compatibility is to set this property:

spring.jpa.properties.hibernate.use-new-id-generator-mappings: false

I do so but it does not seem to take, because the SequenceStyleGenerator is still used. From my understanding, this should cause it to use the SequenceHiLoGenerator. Is this incorrect?

However, if I modify the entity to look like the below it works as expected, replicating the previous functionality I had.

@Entity
@Table(name = "group_link")
@GenericGenerator(
        name = "group_link_seq",
        strategy = "org.hibernate.id.SequenceHiLoGenerator",
                parameters = {
                           @Parameter(name = "sequence_name", value = "group_link_seq"),
                           }
       )
public class GroupLinkEntity extends BaseObject {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "group_link_seq")
    @Column(name = "group_link_id", unique = true, nullable = false)
    private Long id;
    
}

So, it would seem the property is not being taken somehow and I'm looking to figure out why that is. I see it show up in my JpaProperties bean. If I change other properties, like my dialect, I can see that they are taking effect.

Could anyone point me to the code that actually reads that property and makes a decision on what generator to use or point out some obvious error I'm making here?


Solution

  • As it's stated in the documentation:

    You need to ensure that names defined under spring.jpa.properties.* exactly match those expected by your JPA provider. Spring Boot will not attempt any kind of relaxed binding for these entries.

    For example, if you want to configure Hibernate’s batch size you must use spring.jpa.properties.hibernate.jdbc.batch_size. If you use other forms, such as batchSize or batch-size, Hibernate will not apply the setting.

    So, for your case you should use:

    spring.jpa.properties.hibernate.id.new_generator_mappings: false
    

    See also this part of hibernate documentation.