How to use IdGeneratorType to use pooled-lo

I migrated to hibernate 6.5 and noticed deprecation of GenericGenerator, which I used to set optimizer to pooled-lo (AFAIR there is no other way to do it, but I would like to be mistaken).

Basically I have an Oracle tables with sequences as id generators with different sequence names per table (obvious) and sometimes different increment.

Example code I have now (pre 6.5):

...
    private static final String SEQUENCE = "CHANGE_EVENT_SEQ";

    @Id
    @GenericGenerator(
        name = SEQUENCE,
        type = SequenceStyleGenerator.class,
        parameters = {
            @Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = SEQUENCE),
            @Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "5"),
            @Parameter(name = "optimizer", value = "pooled-lo")
        }
    )
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
    private long id;
...

Is it somehow possible to get rid of GenericGenerator and use pooled-lo optimizer?
Or should I migrate to IdGeneratorType?

IdGeneratorType is very poorly documented, all the code samples lack actual code, there are just methods names with params and there aren’t even any imports so I have no clue what packages to use for Sequence or Member (Hibernate ORM 6.0.0.CR1 User Guide):

public class CustomSequenceGenerator implements IdentifierGenerator {

	public CustomSequenceGenerator(
			Sequence config,
			Member annotatedMember,
			CustomIdGeneratorCreationContext context) {
		//...
	}

	@Override
	public Object generate(
			SharedSessionContractImplementor session,
			Object object) {
		//...
}

How to even use that? What should I set where? It would be best if there was a guide (with code) for the section below (2.7.15) “Pooled-lo optimizer mapping using @GenericGenerator mapping” but with IdGeneratorType.

It doesn’t help that there are very few mentions of IdGeneratorType on the web.

Could someone point me to some more detailed documentation? Or explain how I could use pooled-lo with smaller amount of code?

You can configure pooled-lo globally by setting hibernate.id.optimizer.pooled.preferred to pooled-lo, but there is currently no way to configure pooled-lo just for a specific generator.
The code is simply not shown in the documentation to not flood the documentation with these details. You can find the full source code on GitHub though. You can take org.hibernate.id.enhanced.SequenceStyleGenerator as inspiration for building an optimizer into the generator if you want.

Either way, please create an enhancement request for this in our issue tracker and link to this discussion.

1 Like

Thank you, that is very helpful.

So if I set the mentioned property to pooled-lo then deprecated @GenericGenerator could be replaced with jakartas:

private static final String SEQUENCE = "CHANGE_EVENT_SEQ";
@SequenceGenerator(
    name = SEQUENCE,
    sequenceName = SEQUENCE,
    allocationSize = 5
)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
private long id;

Or is there more to GenericGenerator?

Correct.

You can specify parameters for other generators with the @GenericGenerator annotation, but that’s not necessary in your case.

OK, so one last question, because I couldn’t find the answer in the code.

In GeneratedeValue I provide name of the generator to use:

    @GeneratedValue(generator = "FOO_GENERATOR")

In GenericGenerator I used the same name to link those (using name param).

With new approach, I should somehow set that name “somewhere” so Hibernate will be able to link those two annotations, right?
I couldn’t find code related to generator name inside CustomSequenceGenerator nor in SequenceStyleGenerator.

Or to be precise I found CustomIdGeneratorCreationContext (which is passed in constructor of CustomSequenceGenerator) has a method Generator createIdentifierGenerator, but all that code is marked as @Incubating so I’m not encouraged to use it.

The generator name can be specified by the name member of the @SequenceGenerator annotation. Whether the name is global or local to entity hierarchies depends on a configuration option.

You can use that code, just beware the a minor version of Hibernate ORM might change the contract a bit.