2011-12-01 18 views
7

私はherokuにJavaアプリケーションをデプロイしたいと思っています。デプロイされると、環境変数DATABASE_URLが設定されます。私はそれを私の冬眠用のURLとして使用したいと思います。私は現在hibernate.cfg.xmlを持っており、そこにjdbc:postgresql:// localhost:port/dbというURLを設定しています。 DATABASE_URLを使用するように変更するにはどうすればよいですか?環境変数でHibernateを設定するにはどうすればいいですか

答えて

10

setProperty(String propertyName, String value)Configurationを使用して、hibernate.connection.urlの値を明示的に上書きしてから、SessionFactoryを作成します。

環境変数を取得するには、System.getenv(String name)を使用します。

/**Load the hibernate.cfg.xml from the classpath**/ 
Configuration cfg = new Configuration(); 
cfg.setProperty("hibernate.connection.url", System.getenv("DATABASE_URL")); 
SessionFactory sessionFactory = cfg.buildSessionFactory(); 
+0

数分前にもありがとうございました。 :) –

+0

大丈夫かどうか教えてください。幸運:) –

+2

System.getenv( "DATABASE_URL")から返された文字列を微調整する必要があるかもしれません。 –

1

月このヘルプあなた、

私は5.xのはJBoss ASのHSQL DBを使用して、動的にテーブルを作成するために休止状態と* .cfg.xmlファイルを、次の使用しています。

環境変数として$ JBOSS_HOMEを使用しています。

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

<session-factory> 

      <!-- Database connection settings --> 

      <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
      <property name="connection.url">jdbc:hsqldb:$JBOSS_HOME/server/test/data/hypersonic/localDB</property> 
      <property name="connection.username">sa</property> 

      <property name="connection.password"></property> 

      <!-- JDBC connection pool (use the built-in) --> 
      <property name="connection.pool_size">1</property> 

      <!-- SQL dialect --> 
      <property name="dialect">org.hibernate.dialect.HSQLDialect</property> 

      <!-- Enable Hibernate's automatic session context management --> 
      <property name="current_session_context_class">thread</property> 

      <!-- Disable the second-level cache --> 
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

      <!-- Echo all executed SQL to stdout --> 
      <property name="show_sql">true</property> 
      <!-- Drop and re-create the database schema on startup --> 

      <property name="hbm2ddl.auto">update</property> 
      <!-- Mapping files --> 
      <mapping resource="friends_presence_log.hbm.xml" /> 
      <mapping resource="profileuuid.hbm.xml" /> 
    </session-factory> 

ので、その平均あなたは、JBossの設定に環境変数を使用したい場合は、あなたのは、後に内部hibernate.jarユーティリティによって取られ、通常は使用することができますあなたは通常、Javaプログラムのように接続やものを取得します。

4

私は、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> 

に来て、私は次のようVMARGSで自分のアプリケーションを起動します。私は古いフォーラムのポストでこれを見つけたので、私はこの古いポストにこのソリューションを投稿

-Dhibernate_username=test-Dhibernate_password=testpassword -Dhibernate_db_host=localhost -Dhibernate_db_name=test -Dhibernate_show_sql=true 

(Google検索サイド3+ ^^)。そして、これはとても便利だと思います。

関連する問題