我想使用hibernate的TableGenerator进行自定义密钥生成
final SessionImpl session = ((SessionImpl) EntityManagerUtils
.getEntityManager("CORE").getDelegate());
TableGenerator generator = new TableGenerator();
Properties params = new Properties(??????????);
// params.put("identifier_normalizer", ?);
params.put("table_name", "eflow.docs_number_generators");
params.put("value_column_name", "tbl.GENERATOR_VALUE");
params.put("segment_column_name", "tbl.GENERATOR_KEY");
params.put("increment_size", "25");
params.put("segment_value", "2011");
generator.configure(IntegerType.INSTANCE, params, session
.getSessionFactory().getDialect());
Serializable id = generator.generate(session, new Object());
System.out.println(id);
但是缺少具有键“identifier_normalizer”的对象,并且此代码段抛出空指针异常。此对象的类型为ObjectNameNormalizer
,它规范化db对象名称并存储在hibernate本地映射中。
如何访问hibernate的内部映射,从而访问这个ObjectNameNormalizer
对象?
实际上,因为看起来我需要hibernate Mappings对象,我不知道热得到。
提前致谢
答案 0 :(得分:1)
解决方案来了:
private String generateKey() {
final SessionImpl session = ((SessionImpl) em.getDelegate());
if (generator == null) {
synchronized (TableGenerator.class) {
if (generator == null) {
generator = new TableGenerator();
ObjectNameNormalizer normalizer = new ObjectNameNormalizer() {
@Override
protected boolean isUseQuotedIdentifiersGlobally() {
return false;
}
@Override
protected NamingStrategy getNamingStrategy() {
return new Configuration().getNamingStrategy();
}
};
Properties params = new Properties();
params.put("identifier_normalizer", normalizer);
params.put("table_name", "eflow.docs_num_generators");
params.put("value_column_name", "GENERATOR_VALUE");
params.put("segment_column_name", "GENERATOR_KEY");
params.put("increment_size", "25");
params.put("segment_value", "2011");
generator.configure(IntegerType.INSTANCE, params, session
.getSessionFactory().getDialect());
}
}
}
Serializable id = generator.generate(session, new Object());
return id.toString();
}