序列生成器的Hibernate cfg.xml配置 [英] Hibernate cfg.xml configuration for sequence generator
问题描述
我继承了一个具有以下id字段定义的持久化类:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
我的所有类都从这个类继承。
我想要的主要事情:
我需要重写使用HiLo的id生成策略。它必须位于cfg.xml中,因为它是唯一可以控制的地方。
这件事看起来很有希望:
< property name =hibernate.id.new_generator_mappings> true< / property>
然而,我无法选择优化或增量大小(我想去HiLo)
< property name =hibernate.id.increment_size> 50< / property>
< property name =hibernate.id.optimizer> hilo< / property>
不起作用,也不是
< property name =increment_size> 50< / property>
< property name =optimizer> hilo< / property>
也不是
< property name =optimizer> org.hibernate.id.enhanced.HiLoOptimizer< / property>
总是选择NoopOptimizer。
任何提示将有所帮助。
让我们来做一些调查(看看Hibernate的源代码):
AnnotationBinder
定义了AUTO的映射方式:
switch(generatorEnum){
// ...
case AUTO:
return useNewGeneratorMappings?
org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName():native;
// ...
如果您不使用NewGeneratorMappings,与本地实施。 Hibernate会根据数据库来解决这个问题(参见这个答案,看看你的特定数据库将会是什么)。 p>
但是,由于您使用了NewGeneratorMappings,因此我们必须查看 SequenceStyleGenerator
:
protected String determineOptimizationStrategy(Properties params,int incrementSize){
// ...一些计算defaultOptimizerStrategy的东西
// OPT_PARAM =优化器
返回ConfigurationHelper.getString(OPT_PARAM,params,defaultOptimizerStrategy);
假设传递了正确的属性,并且在它们中有一个优化器属性那么你的价值应该返回。此值用于调用 因此,要么找到您的值hilo(或您的HiLoOptimizer) ,实例化它并返回它,否则它会记录一些错误信息。如果您在日志中找不到任何错误消息,我会检查cfg.xml中的属性是否确实在使用。尝试在 Fellow developers,
I'm finding it hard to get the sequence generation configured. I inherited a persistent class with the following id field definition: All of my classes inherit from this class. The main thing I want:
I need to override the strategy of id generation to use HiLo. It has to be in the cfg.xml since it's the only place that I have control over. This thing looked promising: However I couldn't choose the optimize, or the increment size (I want to go HiLo) didn't work, nor nor The NoopOptimizer is always chosen. Any tip will help. Thanks a lot in advance. Let's do some investigation (looking at the Hibernate sources): The If you don't use the NewGeneratorMappings you're stuck with the native implementation. Hibernate will resolve that depending on the database (see this answer to see what that will be for your specific database). But since you used the NewGeneratorMappings we have to look at the Assuming the right Properties are passed and you have an "optimizer" property in them then your value should be returned. This value is used to call So either it finds your value "hilo" (or your "HiLoOptimizer"), instantiates it and returns it or it will log some error message. If you find no error message in your logs I would check whether the properties in the cfg.xml are actually really used. Try to access them with 这篇关于序列生成器的Hibernate cfg.xml配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<$ p $ p> public static Optimizer buildOptimizer(String type,Class returnClass,int incrementSize){
final Class <?扩展优化程序> 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);
返回buildFallbackOptimizer(returnClass,incrementSize);
try {
final构造函数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);
$ / code> org.hibernate.internal.SessionFactoryImpl
实例上使用 getProperties()
访问它们。@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
<property name="hibernate.id.new_generator_mappings">true</property>
<property name="hibernate.id.increment_size">50</property>
<property name="hibernate.id.optimizer">hilo</property>
<property name="increment_size">50</property>
<property name="optimizer">hilo</property>
<property name="optimizer">org.hibernate.id.enhanced.HiLoOptimizer</property>
AnnotationBinder
defines how AUTO is mapped:switch ( generatorEnum ) {
// ...
case AUTO:
return useNewGeneratorMappings ?
org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName() : "native";
// ...
SequenceStyleGenerator
:protected String determineOptimizationStrategy(Properties params, int incrementSize) {
// ... some stuff to calculate defaultOptimizerStrategy
// OPT_PARAM = "optimizer"
return ConfigurationHelper.getString( OPT_PARAM, params, defaultOptimizerStrategy);
}
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 );
}
getProperties()
on your org.hibernate.internal.SessionFactoryImpl
instance.