所以我想在heroku上部署我的java应用程序。部署后,它会设置一个环境变量DATABASE_URL。我想用它作为我的hibernate网址。我目前有hibernate.cfg.xml,并在那里设置了url jdbc:postgresql:// localhost:port / db。如何更改它以获取DATABASE_URL?
答案 0 :(得分:11)
其中一种方法是在创建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();
答案 1 :(得分:9)
我在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 Search Side 3+ ^^)中找到了这个。我认为这非常有用。
答案 2 :(得分:1)
愿这对你有帮助,
我正在使用Jboss AS 5.x和hibernate的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程序一样得到连接或事物。
答案 3 :(得分:1)
如果使用诸如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>
然后在 build.gradle 中,您将遇到类似这样的事情:
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,
]
}
}