Hibernate cfg.xml configuration for sequence generator

Developers, It's hard for me to set up a sequence configuration.

I inherited a persistent class with the following id field definition:

@Id @GeneratedValue(strategy= GenerationType.AUTO) private Long id; 

All my classes inherit this class.

The main thing I want: I need to redefine the id generation strategy to use HiLo. This should be in cfg.xml as this is the only place I control.

This thing looked promising:

 <property name="hibernate.id.new_generator_mappings">true</property> 

However, I could not choose the optimization or the size of the increment (I want to go HiLo)

 <property name="hibernate.id.increment_size">50</property> <property name="hibernate.id.optimizer">hilo</property> 

not working as well

 <property name="increment_size">50</property> <property name="optimizer">hilo</property> 

neither

 <property name="optimizer">org.hibernate.id.enhanced.HiLoOptimizer</property> 

NoopOptimizer is always selected.

Any advice would help. Thank you very much in advance.

+3
hibernate hibernate-mapping
30 Oct '13 at 21:29
source share
2 answers

Do some research (looking at Hibernate sources):

AnnotationBinder defines how AUTO is displayed:

 switch ( generatorEnum ) { // ... case AUTO: return useNewGeneratorMappings ? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName() : "native"; // ... 

If you are not using NewGeneratorMappings, you are stuck in your own implementation. Hibernate will decide which one depending on the database (see this answer to find out what will be for your specific database).

But since you used NewGeneratorMappings, we should look at SequenceStyleGenerator :

 protected String determineOptimizationStrategy(Properties params, int incrementSize) { // ... some stuff to calculate defaultOptimizerStrategy // OPT_PARAM = "optimizer" return ConfigurationHelper.getString( OPT_PARAM, params, defaultOptimizerStrategy); } 

Assuming the correct properties are passed, and you have the optimizer property, then your value should be returned. This value is used to call OptimizerFactory.buildOptimizer (as the first parameter):

  public static Optimizer buildOptimizer(String type, Class returnClass, int incrementSize) { final Class<? extends Optimizer> optimizerClass; final StandardOptimizerDescriptor standardDescriptor = StandardOptimizerDescriptor.fromExternalName( type ); // HILO("hilo", HiLoOptimizer.class), if ( standardDescriptor != null ) { optimizerClass = standardDescriptor.getOptimizerClass(); } else { try { optimizerClass = ReflectHelper.classForName( type ); } catch( Throwable ignore ) { LOG.unableToLocateCustomOptimizerClass( type ); return buildFallbackOptimizer( returnClass, incrementSize ); } } try { final Constructor ctor = optimizerClass.getConstructor( CTOR_SIG ); return (Optimizer) ctor.newInstance( returnClass, incrementSize ); } catch( Throwable ignore ) { LOG.unableToInstantiateOptimizer( type ); } return buildFallbackOptimizer( returnClass, incrementSize ); } private static Optimizer buildFallbackOptimizer(Class returnClass, int incrementSize) { return new NoopOptimizer( returnClass, incrementSize ); } 

That way, either it finds your hilo value (or your HiLoOptimizer), creates an instance and returns it, or it logs some error message. If an error message does not appear in your logs, I would check if the properties in cfg.xml are actually used . Try to access them using getProperties() on your instance of org.hibernate.internal.SessionFactoryImpl .

+2
Nov 09 '13 at 19:08
source share
β€” -

This is not yet possible. but also I could not find a way to do this with the new

 hibernate.id.new_generator_mappings = true 

This one will use the new SequenceStyleGenerator, but I could not configure it. Parameters used for configuration, such as the optimizer and increment_size, are not passed to the method that the generator sets.

I want to say that I don’t know how this "hibernate.id.new_generator_mappings" should be used to use the hilo optimizer and is properly configured, and my reverse engineering capabilities have been unreliable.

To use hilo, I had to do this @GeneratedValue (strategy = SEQUENCE), which works for testing, but this is not a good option for me.

Thanks @xwoker, but I could not do this.

0
Nov 13 '13 at 17:15
source share



All Articles