2

我想使用休眠的 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标准化数据库对象名称的类型,并存储在休眠本地映射中。

如何访问hibernate的内部映射和这个ObjectNameNormalizer对象?

实际上,我似乎需要休眠映射对象,但我不知道如何获得它。

提前致谢

4

1 回答 1

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();
}
于 2012-11-07T10:25:32.187 回答