java - Hibernate 向目录添加自定义前缀

标签 java database hibernate configuration schema

我正在使用 Hibernate 4.3.6,我需要根据环境向我的目录添加自定义前缀。我曾经在 4.2.3 版本中执行此代码

private static SessionFactory buildSessionFactory() {
    try {
        Configuration config = new Configuration();
        config.configure("db.cfg.xml");
        config.buildMappings();

        ServiceRegistry registry = new StandardServiceRegistryBuilder()
                .applySettings(config.getProperties())
                .build();

        if (prefix != null && !prefix.isEmpty()) {
            Iterator<Table> iterator = config.getTableMappings();
            while (iterator.hasNext()) {
                Table table = (Table) iterator.next();
                table.setCatalog(prefix + table.getCatalog());
            }
        }
        //return new AnnotationConfiguration().buildSessionFactory(registry);
        SessionFactory factory = config.buildSessionFactory(registry);
        return factory;
    }
    catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println(ex);
        throw new ExceptionInInitializerError(ex);
    }
}

但是现在它出错了

config.buildSessionFactory(registry);

java.lang.ExceptionInInitializerError
at db.DatabaseEngine.buildSessionFactory(DatabaseEngine.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1456)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at db.DatabaseEngine.buildSessionFactory(DatabaseEngine.java:104)
... 26 more

是否有像 ImprovedNamingStrategy 这样的东西,但用于目录?

最佳答案

创建 orm.xml 后,您可以使用以下代码加载它:

Configuration config = new Configuration();
config.configure("db.cfg.xml");
config.addResource("orm.xml"); // Load these files in any order
config.buildMappings();

有关如何创建 orm.xml 文件的信息,请参见 Vlad 的回答。

关于java - Hibernate 向目录添加自定义前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26610015/

相关文章:

java - 如何计算按钮的点击次数并保存在 MySql 数据库中?新 session 中的点击次数应添加到现有值中

java - 使用 @OneToMany 注释关系时是否可以定义 LEFT Join?

java - 邮件未呈现 html 并且格式损坏

iphone - HTML5 数据库验证表是否存在?

mysql - 外键约束 : When to use ON UPDATE and ON DELETE

php - 数据库之间的重复值

java - 如何在没有 Spring 的情况下启用 hibernate 字节码增强

java - 使用 Apache POI 确定 MS Excel 文件类型

java - 为什么 PostConstruct 方法在 springboot 中使用时不能保持 bean init 顺序?

java - 在 Spring Boot 应用程序中添加 Angular