postgresql 如何使用环境变量配置 Hibernate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8349717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I configure Hibernate with environment variable
提问by Nathan Drake
So i want to deploy my java app on heroku. Once deployed it sets an environment variable DATABASE_URL. I want to use that as my url for hibernate. I currently have hibernate.cfg.xml and there i set the url jdbc:postgresql://localhost:port/db like this . How can i change it to take DATABASE_URL?
所以我想在heroku上部署我的java应用程序。部署后,它会设置一个环境变量 DATABASE_URL。我想用它作为我的休眠网址。我目前有 hibernate.cfg.xml 并且我在那里设置了 url jdbc:postgresql://localhost:port/db 像这样。如何更改它以获取 DATABASE_URL?
回答by kdoteu
I searched a lot for another solution without programming anything in java itself. I came to the following conclusion.
我搜索了很多另一种解决方案,而没有在 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>
And I start my application with following vmargs:
我使用以下 vmargs 启动我的应用程序:
-Dhibernate_username=test -Dhibernate_password=testpassword -Dhibernate_db_host=localhost -Dhibernate_db_name=test -Dhibernate_show_sql=true
I post this solution to this old Post, because I found this in an old forum post (Google Search Side 3+ ^^). And I think this is very useful.
我将此解决方案发布到这个旧帖子,因为我在旧论坛帖子(Google Search Side 3+ ^^)中找到了它。我认为这非常有用。
回答by Ken Chan
One of the ways is to use setProperty(String propertyName, String value)of Configurationto explicitly override the value of hibernate.connection.url
before creating the SessionFactory.
其中一种方法是使用的setProperty(字符串propertyName的,字符串值)的配置,明确覆盖的价值hibernate.connection.url
创造的SessionFactory之前。
To get the environment variables , you can use System.getenv(String name).
要获取环境变量,您可以使用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();
回答by pkm1986
May this help you,
愿这对你有帮助,
I am using HSQL DB of Jboss AS 5.x and hibernate to dynamically create tables and using following *.cfg.xml file.
我正在使用 Jboss AS 5.x 和 hibernate 的 HSQL DB 来动态创建表并使用以下 *.cfg.xml 文件。
which is using $
JBOSS_HOME as environment variable.
使用$
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>
So, Its mean if you want to use environment variable into Jboss config then your can use normally which later taken by internal hibernate.jar utility and you get connection or things normally as in a java program.
因此,这意味着如果您想在 Jboss 配置中使用环境变量,那么您可以正常使用,稍后会被内部 hibernate.jar 实用程序采用,并且您可以像在 Java 程序中一样正常获得连接或其他东西。
回答by Jose Solorzano
If you use a build tool like gradle, you can use placeholders enclosed by @...@ as follows:
如果你使用像 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>
Then in build.gradleyou'd have something like this:
然后在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,
]
}
}