我正在使用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这样的东西,但对于目录?
答案 0 :(得分:1)
根据Hibernate Annotation docs,orm.xml XML设置可以覆盖注释配置:
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>${db.schema}</schema>
<catalog>${db.catalog}</catalog>
</persistence-unit-defaults>
</persistence-unit-metadata>
<entity class="package.YourEntity1">
<table name="YOUR_TABLE_1" catalog="${specific_catalog1}"/>
</entity>
<entity class="package.YourEntity2">
<table name="YOUR_TABLE_2" catalog="${specific_catalog2}"/>
</entity>
架构/目录变量可以由Maven根据您在构建之前运行的特定配置文件替换。这适用于默认目录和特定实体目录定义。
答案 1 :(得分:1)
创建orm.xml后,可以使用以下代码加载它:
Configuration config = new Configuration();
config.configure("db.cfg.xml");
config.addResource("orm.xml"); // Load these files in any order
config.buildMappings();
请参阅Vlad关于如何创建orm.xml文件的答案。
答案 2 :(得分:0)
每个环境加载配置文件不会更简单,我的意思是(取决于env)
config.configure("db.cfg-env1.xml");
或
config.configure("db.cfg-env2.xml");
并在jdbc连接url中设置了正确的目录?
接近实施的例子(thx serge让我知道:))
config.configure(String.format("db.cfg-%s.xml"), env);