如何使用环境变量配置Hibernate

如何使用环境变量配置Hibernate,hibernate,postgresql,configuration,heroku,Hibernate,Postgresql,Configuration,Heroku,所以我想在heroku上部署我的java应用程序。一旦部署,它将设置一个环境变量DATABASE_URL。我想用它作为hibernate的url。我现在有hibernate.cfg.xml,在那里我设置了url jdbc:postgresql://localhost:port/db 这样地。如何将其更改为获取数据库URL?其中一种方法是在创建SessionFactory之前使用显式重写hibernate.connection.URL的值 要获取环境变量,可以使用 希望这对你有帮助 我使用Jbos

所以我想在heroku上部署我的java应用程序。一旦部署,它将设置一个环境变量DATABASE_URL。我想用它作为hibernate的url。我现在有hibernate.cfg.xml,在那里我设置了url jdbc:postgresql://localhost:port/db 这样地。如何将其更改为获取数据库URL?

其中一种方法是在创建SessionFactory之前使用显式重写
hibernate.connection.URL的值

要获取环境变量,可以使用

希望这对你有帮助

我使用Jboss AS 5.x的HSQL DB和hibernate动态创建表,并使用以下*.cfg.xml文件

它使用
$
JBOSS\u HOME作为环境变量

<?xml version="1.0" encoding="UTF-8"?>


org.hsqldb.jdbcDriver
jdbc:hsqldb:$JBOSS_HOME/server/test/data/hypersonic/localDB
sa
1.
org.hibernate.dialogue.hsql方言
线
org.hibernate.cache.NoCacheProvider
真的
更新


所以,这意味着,如果您想在Jboss配置中使用环境变量,那么您可以正常使用内部hibernate.jar实用程序所使用的环境变量,并且您可以像在java程序中一样正常地获得连接或其他东西。

我搜索了很多其他解决方案,但没有使用java本身编程。 我得出以下结论

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.check_nullability">false</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.username">${hibernate_username}</property>
    <property name="hibernate.connection.password">${hibernate_password}</property>
    <property name="hibernate.connection.url">jdbc:postgresql://${hibernate_db_host}/${hibernate_db_name}</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.show_sql">${hibernate_show_sql}</property>
</session-factory>
</hibernate-configuration>

我将此解决方案发布到这篇旧帖子中,因为我在一篇旧论坛帖子(Google Search Side 3+^^)中找到了此解决方案。我认为这是非常有用的。

如果你使用像gradle这样的构建工具,你可以使用由@…@括起来的占位符,如下所示:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name = "hibernate.dialect">
         @db_dialect@
      </property>

      <property name = "hibernate.connection.driver_class">
         @db_driver@
      </property>

      <!-- Assume test is the database name -->

      <property name = "hibernate.connection.url">
         @db_url@
      </property>

      <property name = "hibernate.connection.username">
         @db_user@
      </property>

      <property name = "hibernate.connection.password">
         @db_password@
      </property>

      <!-- Cannot use anything other than "validate" in production. --> 
      <property name="hibernate.hbm2ddl.auto">validate</property>

      <!-- Connection pooling -->
      <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
      <property name="hibernate.c3p0.min_size">1</property>
      <property name="hibernate.c3p0.max_size">500</property>
      <property name="hibernate.c3p0.timeout">120</property>
      <property name="hibernate.c3p0.max_statements">1024</property>

      <!-- Your mappings -->
   </session-factory>
</hibernate-configuration>

非常感谢您几分钟前就想好了。:)请告诉我是否可以。祝您好运:)您可能需要调整从System.getenv(“DATABASE_URL”)返回的字符串。使用$VAR不会得到解析为什么我们需要将它们作为VM参数传入,而不仅仅是在环境变量中设置它们?在我们的用例中,hibernate配置文件打包在一个jar文件中。我们的客户编辑这些文件有点乏味。这就是我们将它们打包在VM参数中的原因,这些参数更易于配置。感谢您对一个老主题的回复!是否有任何方法可以像您一样设置它,但不使用VMargs而使用环境变量?
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.check_nullability">false</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.username">${hibernate_username}</property>
    <property name="hibernate.connection.password">${hibernate_password}</property>
    <property name="hibernate.connection.url">jdbc:postgresql://${hibernate_db_host}/${hibernate_db_name}</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.show_sql">${hibernate_show_sql}</property>
</session-factory>
</hibernate-configuration>
-Dhibernate_username=test -Dhibernate_password=testpassword -Dhibernate_db_host=localhost -Dhibernate_db_name=test -Dhibernate_show_sql=true
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name = "hibernate.dialect">
         @db_dialect@
      </property>

      <property name = "hibernate.connection.driver_class">
         @db_driver@
      </property>

      <!-- Assume test is the database name -->

      <property name = "hibernate.connection.url">
         @db_url@
      </property>

      <property name = "hibernate.connection.username">
         @db_user@
      </property>

      <property name = "hibernate.connection.password">
         @db_password@
      </property>

      <!-- Cannot use anything other than "validate" in production. --> 
      <property name="hibernate.hbm2ddl.auto">validate</property>

      <!-- Connection pooling -->
      <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
      <property name="hibernate.c3p0.min_size">1</property>
      <property name="hibernate.c3p0.max_size">500</property>
      <property name="hibernate.c3p0.timeout">120</property>
      <property name="hibernate.c3p0.max_statements">1024</property>

      <!-- Your mappings -->
   </session-factory>
</hibernate-configuration>
def db_driver = System.getenv('MYAPP_DB_DRIVER')
def db_url = System.getenv('MYAPP_DB_URL')
def db_dialect = System.getenv('MYAPP_DB_DIALECT')

processResources {
    filesMatching(['**/*.xml', '**/*.properties', '**/*.json']) {
        filter ReplaceTokens, tokens: [
            'db_driver': db_driver,
            'db_url': db_url,
            'db_dialect': db_dialect,
        ]
    }
}