# Deprecation of @GenericGenerator in Hibernate 6.5

**URL:** <https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482>\
**Category:** Hibernate ORM\
**Created:** [May 7, 2024, 4:24pm UTC](https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482 "2024-05-07T16:24:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ky2help](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ky2help/32/4085_2.png) [@ky2help](https://discourse.hibernate.org/u/ky2help)\
**Post date:** [May 7, 2024, 4:24pm UTC](https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482/1 "2024-05-07T16:24:02Z")

</div>

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:

```java
/**
 * ...
 * 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](https://vladmihalcea.com/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.

```java
@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
<?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](https://docs.jboss.org/hibernate/orm/6.0/migration-guide/migration-guide.html#_deprecation_of_hbm_xml_mappings), right?

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

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [May 9, 2024, 8:17am UTC](https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482/2 "2024-05-09T08:17:27Z")

</div>

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:

```java
@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](https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-IdGeneratorType).

---

<div class="post-metadata">

**Author:** ![ky2help](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ky2help/32/4085_2.png) [@ky2help](https://discourse.hibernate.org/u/ky2help)\
**Post date:** [May 13, 2024, 12:09pm UTC](https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482/3 "2024-05-13T12:09:48Z")

</div>

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)](https://github.com/ky2help/hibernate-test-case-override-identifier-generator)

Is there anything important that we might have missed?

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [May 13, 2024, 1:13pm UTC](https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482/4 "2024-05-13T13:13:13Z")

</div>

> [@ky2help](#):
>
> We tried your suggestion in advance regardless of the note in the JavaDoc, but unfortunately it didn’t work

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](https://hibernate.atlassian.net/browse/HHH) and attach it.

---

<div class="post-metadata">

**Author:** ![ky2help](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ky2help/32/4085_2.png) [@ky2help](https://discourse.hibernate.org/u/ky2help)\
**Post date:** [May 14, 2024, 7:12am UTC](https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482/5 "2024-05-14T07:12:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ky2help](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ky2help/32/4085_2.png) [@ky2help](https://discourse.hibernate.org/u/ky2help)\
**Post date:** [May 17, 2024, 9:33am UTC](https://discourse.hibernate.org/t/deprecation-of-genericgenerator-in-hibernate-6-5/9482/6 "2024-05-17T09:33:30Z")

</div>

We created the bug [HHH-18124](https://hibernate.atlassian.net/browse/HHH-18124) for further investigation.
