我正在使用 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/