I am trying to migrate my code from Hibernate 4.3 to 5.0. In Hibernate 4.3 I used a SequenceStyleGenerator to get numbers from a sequence with the following code:
Code:
fGenerator = new SequenceStyleGenerator();
Properties params = new Properties();
params.setProperty(SequenceStyleGenerator.SEQUENCE_PARAM, sequenceName);
params.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizer() {
@Override
protected boolean isUseQuotedIdentifiersGlobally() {
return false;
}
@Override
protected NamingStrategyDelegator getNamingStrategyDelegator() {
return ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE;
}
@Override
protected NamingStrategy getNamingStrategy() {
return DefaultNamingStrategy.INSTANCE;
}
});
fGenerator.configure(StandardBasicTypes.LONG, params, dialect);
In hibernate 5.0 both the ObjectNameNormalizer and the configure method have changed. I tried the following
Code:
fGenerator = new SequenceStyleGenerator();
Properties params = new Properties();
params.setProperty(SequenceStyleGenerator.SEQUENCE_PARAM, sequenceName);
params.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizer() {
@Override
protected MetadataBuildingContext getBuildingContext() {
return null;
}
});
fGenerator.configure(StandardBasicTypes.LONG, params, registry);
This does not work. I found out that in order make the generator work the method registerExportables(Database database) must be called. But I don't know where I can get the correct object of type Database from. I should somehow get it from the configuration but I don't know how. Or do I have to register my sequence object so that the registerExportables method is called for me?
I also don't know what kind of ObjectNameNormalizer I should use or what MetadataBuildingContext I should return.
Does anyone know how to use sequences without using entities or JPA?