I am using spring boot 3.3.2, hibernate 6 spring data jpa.
Please understand that I am new to jpa and hibernate.
I want to use the identity strategy when using mariadb lower version than 10.3, use the sequence strategy when using mariadb 10.3 or higher, and use the sequence strategy when using oracle. .
Even if you set @GeneratedValue(strategy = GenerationType.AUTO), Hibernate attempts to use the sequence strategy even though there is no sequence function in mariadb 10.3 or lower.
In order to not be dependent on any dbms, I try not to use @GeneratedValue(strategy = GenerationType.IDENTITY) or @GeneratedValue(strategy = GenerationType.SEQUENCE). I wonder if there is a way to set the default @GeneratedValue(strategy = GenerationType.AUTO) in the entity and adjust the strategy in the configuration file.
The settings I used are as follows.
// using mariadb lower version than 10.3
hibernate.id.new_generator_mappings: false
hibernate.id.create_generator_mappings: false
hibernate.id.db_structure_naming_strategy: legacy (I tried using this setting and tried using hibernate_sequence, but it did not help.)
hibernate.id.generator.strategy: identity
When using GenerationType.AUTO, Hibernate will default to the sequence style generator when they are supported by the underlying database, are you saying that this is not working with MariaDB? Note that the latest supported version of MariaDB since Hibernate 6.4+ is 10.4.
You can implement a custom generator logic which can be configured as you please through your own business logic, but the Hibernate-native generators should work with supported database vendors.
Thanks for your answer. Let me ask you one more thing.
I changed the dialects to match the version of mariadb as shown below.
add to build.gradle
implementation ‘org.hibernate.orm:hibernate-community-dialects:6.5.2.Final’
add to property
hibernate.dialect > org.hibernate.community.dialect.MariaDBLegacyDialect
hibernate.id.new_generator_mappings > false
the table strategy will be adopted.
(I found out that starting from Hibernate 5, if you specify auto, the table strategy will be used by default.)
Is there a way to adopt mariadb’s identity strategy as the application.yml setting? Is there any other way to create custom generator logic?