没有开箱即用的 webapp 特定 部署属性。
但您可以设置您在 persistence.xml 中引用的特定于 webapp 的系统属性:
<persistence-unit >
...
<properties>
<property name="hibernate.hbm2ddl.auto" value="${mywebapp.hibernate.hbm2ddl.auto}" />
</properties>
</persistence-unit>
可以在standalone.conf(.bat) 或standalone.xml 中设置系统属性:
<server xmlns="urn:jboss:domain:3.0">
<extensions> ... </extensions>
<system-properties>
<property name="mywebapp.hibernate.hbm2ddl.auto" value="create"/>
...
</system-properties>
...
</server>
唯一的缺点:您必须在每个环境中设置系统属性 - 没有默认值。
另一个选项是创建一个在设置中设置值的积分器。不幸的是,配置在开始时被读入 Settings 对象,Settings.setAutoCreateSchema() 和其他 hibernate.hbm2ddl.auto 特定属性是包受保护,但您可以使用反射设置它们:
public class AutoCreateSchemaIntegrator implements Integrator {
@Override
public void integrate(Configuration config, SessionFactoryImplementor factory,
SessionFactoryServiceRegistry registry) {
Settings settings = factory.getSettings();
try {
Method setter = settings.getClass().getDeclaredMethod("setAutoCreateSchema", boolean.class);
setter.setAccessible(true);
setter.invoke(settings, myDeploymentSpecificProperty);
} catch (ReflectiveOperationException | SecurityException e) {
// handle exception
}
}
}
您需要将该集成器的全限定类名写入META-INF/services/org.hibernate.integrator.spi.Integrator:
com.myproject.AutoCreateSchemaIntegrator
如果您想动态确定 hbm2ddl 设置,这是最佳选择。