如何在Wildfly中部署时使用persistence.xml设置hibernate.hbm2ddl.auto [英] How to set hibernate.hbm2ddl.auto on deployment in Wildfly NOT using persistence.xml

查看:228
本文介绍了如何在Wildfly中部署时使用persistence.xml设置hibernate.hbm2ddl.auto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为使用JPA的webapp生成部署模式。
服务器是Wildfly 9,Hibernate是JPA提供商。



我可以通过添加



<$来实现这一点p $ p> < property name =hibernate.hbm2ddl.autovalue =create/>

到persistence.xml。



是还有另一种方法可以在Wildfly 9中设置此属性每个webapp 部署?我也尝试了hibernate.properties,但这没有效果。

解决方案

没有特定于webapp的部署属性开箱即用。



但是你可以设置你在persistence.xml中引用的特定于webapp的系统属性:

 < persistence-unit> 
...
< properties>
< property name =hibernate.hbm2ddl.autovalue =$ {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.autovalue =create/>
...
< / system-properties>
...
< / server>

唯一的缺点:您必须在每个环境中设置系统属性 - 没有默认值。



另一种选择是创建一个Integrator来设置设置中的值。不幸的是,配置在开头读入 Settings 对象,而 Settings.setAutoCreateSchema()和其他 hibernate.hbm2ddl.auto 特定的属性是包受保护,但您可以使用反射设置它们:

 公共类AutoCreateSchemaIntegrator实现Integrator {

@覆盖
public void integrate(配置配置,SessionFactoryImplementor工厂,
SessionFactoryServiceRegistry注册表){
Settings settings = factory.getSettings();

try {
Method setter = settings.getClass()。getDeclaredMethod(setAutoCreateSchema,boolean.class);
setter.setAccessible(true);
setter.invoke(settings,myDeploymentSpecificProperty);
} catch(ReflectiveOperationException | SecurityException e){
//处理异常
}
}
}

您需要将该集成商的完全限定类名写入 META-INF / services / org.hibernate.integrator.spi.Integrator

  com.myproject.AutoCreateSchemaIntegrator 

如果你想动态确定hbm2ddl设置,这是最好的选择。


I need on deployment schema generation for a webapp that uses JPA. The server is Wildfly 9 with Hibernate as JPA provider.

I can achieve this by adding

<property name="hibernate.hbm2ddl.auto" value="create" />

to persistence.xml.

Is there another way to set this property per webapp and on deployment in Wildfly 9? I also tried hibernate.properties, but this had no effect.

解决方案

There is no webapp specific deployment property out of the box.

But you can set a webapp specific system property which you reference in your persistence.xml:

<persistence-unit >
  ...
  <properties>
    <property name="hibernate.hbm2ddl.auto" value="${mywebapp.hibernate.hbm2ddl.auto}" />
  </properties>
</persistence-unit>

A system property can be set in the standalone.conf(.bat) or in the standalone.xml:

<server xmlns="urn:jboss:domain:3.0"> 
  <extensions> ... </extensions>
  <system-properties>
    <property name="mywebapp.hibernate.hbm2ddl.auto" value="create"/>
    ...
  </system-properties>
  ...
</server>

Only drawback: you will have to set the system property in every environment - there is no default value.

Another option is to create an Integrator which sets the value in the settings. Unfortunately the config is read into the Settings object at the beginning and Settings.setAutoCreateSchema() and the other hibernate.hbm2ddl.auto specific properties are package protected, but you can set them with reflection:

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
    }
  }
}

You need to write the full qualified class name of that integrator into META-INF/services/org.hibernate.integrator.spi.Integrator:

com.myproject.AutoCreateSchemaIntegrator

This is the best option if you want to determine the hbm2ddl setting dynamically.

这篇关于如何在Wildfly中部署时使用persistence.xml设置hibernate.hbm2ddl.auto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆