2011-12-01 157 views
7

所以我想部署我的Java應用程序在heroku上。一旦部署,它設置一個環境變量DATABASE_URL。我想用它作爲我的hibernate網址。我目前有hibernate.cfg.xml,並在那裏設置URL jdbc:postgresql:// localhost:port/db像這樣。我如何將其更改爲DATABASE_URL?如何配置Hibernate的環境變量

回答

10

其中一種方法是在創建SessionFactory之前使用setProperty(String propertyName, String value)Configuration明確覆蓋hibernate.connection.url的值。

要獲取環境變量,可以使用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

願這幫助你,

我使用JBoss的HSQL DB AS 5.x和休眠動態創建表,並使用下面的* .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本身編程anythin。 我來到了以下結論

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

我張貼這種解決這個老帖子,因爲我發現這個在老論壇帖子(谷歌搜索Side 3+ ^^)。我認爲這非常有用。