hibernate - 如何使用环境变量配置 Hibernate

标签 hibernate postgresql configuration heroku

所以我想在 heroku 上部署我的 java 应用程序。部署后,它会设置一个环境变量 DATABASE_URL。我想用它作为 hibernate 的 url。我目前有 hibernate.cfg.xml,我在那里设置了 url jdbc:postgresql://localhost:port/db 这样的。我如何更改它以使用 DATABASE_URL?

最佳答案

我搜索了很多其他解决方案,但没有用 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+ ^^)中找到了它。我认为这非常有用。

关于hibernate - 如何使用环境变量配置 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8349717/

相关文章:

sql - 使用 PostgreSQL 的 PgAdmin-III 导出数据库

java - 从文本文件中命名 R 中的变量

configuration - 是否可以将 TeamCity 构建代理配置为仅运行具有特定参数依赖性的构建?

java - 使用 CriteriaBuilder 的动态查询 JPA 2 规范

sql - 从 Slick 3.x 中的分组依据中按列选取最大项目

java - 运行 Junit 时的 Hibernate ConstraintValidation

django - 并行线程不会增加具有 Django 性能的多个数据库插入

python - 可插拔应用程序的Django默认设置约定?

Hibernate更新主键,批量更新从update [0]返回意外的行数;实际行数 : 0; expected: 1

java - 引用 transient 实体时出现 Hibernate TransientObjectException