Deprecation of @GenericGenerator in Hibernate 6.5

In Hibernate 6.5 the @GenericGenerator has been deprecated in favor of @IdGeneratorType, which according to the documentation is not supposed to work with the JPA-defined annotation jakarta.persistence.GeneratedValue:

JavaDoc for @IdGeneratorType annotation:

/**
 * ...
 * and we may use it as follows:
 * <pre>
 * &#64;Id &#64;CustomSequence(name = "mysequence", startWith = 0)
 * private Integer id;
 * </pre>
 * <p>
 * We did not use the JPA-defined {@link jakarta.persistence.GeneratedValue}
 * here, since that API is designed around the use of stringly-typed names.
 * The {@code @CustomSequence} annotation itself implies that {@code id} is
 * a generated value.
 */

Our application uses Hibernate with a Sequence- or an Identity-Strategy depending on the DBMS in production. Prior to Hibernate 6.5 we relied on the article from Vlad Mihalcea: How to replace the TABLE identifier generator with either SEQUENCE or IDENTITY in a portable way, by defining the default mapping using java code with @GeneratedValue. Additional XML metadata is provided at runtime to override the strategy. For MySQL we still use the Identity-Strategy as there is no native support for Sequences yet.

@MappedSuperclass
public abstract class AbstractEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = CustomSequenceGenerator.STRATEGY_NAME)
    @GenericGenerator(name = CustomSequenceGenerator.STRATEGY_NAME, type = CustomSequenceGenerator.class, parameters = {
            @Parameter(name = "initial_value", value = "1"),
            @Parameter(name = "increment_size", value = "1")
    })
    @Nullable
    public Long getId() {
        return id;
    }
    
    ...
    
}
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="https://jakarta.ee/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence/orm https://jakarta.ee/xml/ns/persistence/orm/orm_3_1.xsd"
                 version="3.1">
    <mapped-superclass class="org.example.AbstractEntity">
        <attributes>
            <id name="id">
                <generated-value strategy="IDENTITY"/>
            </id>
        </attributes>
    </mapped-superclass>
</entity-mappings>

@GeneratedValue is part of JPA, so it is also available using XML metadata. The new @IdGeneratorType is part of Hibernate, which can’t be used in XML because the hbm.xml mappings are deprecated in Hibernate 6.0, right?

Is there a way to achieve a portable strategy with the new @IdGeneratorType annotation?

If I understand your use-case correctly, in some cases you are overriding the configured identifier generator with an xml mapping that uses <generated-value strategy="IDENTITY"/>. That should still work in Hibernate 6.0, and is not related with the deprecation of @GenericGenerator.

In your annotation mappings, you can use the new @IdGenerationType meta-annotation to specify your custom sequence generator class like so:

@IdGeneratorType( CustomSequenceGenerator.class )
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface SequenceGenerated {
	String name();
	int startWith() default 1;
	int incrementBy() default 50;
}

@MappedSuperclass
public abstract class AbstractEntity {
    
    @Id
    @SequenceGenerated( name="custom_sequence", incrementBy = 1 )
    public Long getId() {
        return id;
    }

}

See also this user guide chapter.

Yes, we want to override the configured identifier generator in some cases. We tried your suggestion in advance regardless of the note in the JavaDoc, but unfortunately it didn’t work. If it helps, the differences between the two variants are available on GitHub:

Test cases (GitHub)

Is there anything important that we might have missed?

What didn’t work? Are you getting any errors? Are the generator configurations not being applied? Thanks for sharing a reproducer, if you think this is a bug feel free to open a new ticket in our issue tracker and attach it.

We didn’t get any errors, the XML override is not applied if we use the new @IdGeneratorType annotation. The test cases previous published on GitHub should be sufficient to confirm the difference between @GenericGenerator and @IdGeneratorType.

With regard to the note in the JavaDoc at @IdGeneratorType it was not clear to us whether this behavior is intended or not, since the @GeneratedValue is no longer used:

We did not use the JPA-defined jakarta.persistence.GeneratedValue here, since that API is designed around the use of stringly-typed names. The @CustomSequence annotation itself implies that id is a generated value.

If you believe that, for example, the XML override <generated-value strategy="IDENTITY"/> should also work with the new @IdGeneratorType annotation, even if the @GeneratedValue annotation is not present, like you described before, then it should be a bug right? Shall we open a new ticket?

We created the bug HHH-18124 for further investigation.

1 Like