1

I am new here, I used this site to look for answers for a long time, but this is the first time I would like to ask a question. I want to deploy my Spring (not Spring Boot) application using Heroku. I found a very nice tutorial (https://github.com/Abdallah-Abdelazim/yt-heroku-demo/blob/master/README.txt), but it only shows how to do that with a Spring Boot application. His application.properties file looks like this:

  spring.datasource.url=${JDBC_DATABASE_URL}
  spring.datasource.username=${JDBC_DATABASE_USERNAME}
  spring.datasource.password=${JDBC_DATABASE_PASSWORD}
  spring.jpa.show-sql=false
  spring.jpa.generate-ddl=true
  spring.jpa.hibernate.ddl-auto=create

I think that what I should do is to edit my persistence.xml accordingly. I did something like this, but it didn't work:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="passwordsPersistenceUnit">
        <properties>
            <property name="javax.persistence.jdbc.url"
                      value="${JDBC_DATABASE_URL}"/>
            <property name="javax.persistence.jdbc.user" value="${JDBC_DATABASE_USERNAME}"/>
            <property name="javax.persistence.jdbc.password" value="${JDBC_DATABASE_PASSWORD}"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>

            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
                <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

How can I change it to make it work? Thank you very much in advance.

1

1 Answer 1

2

The persistence.xml doesn't support env vars as you've discovered. You'll need to do it in code, something like:

Map<String, String> env = System.getenv();
Map<String, Object> configOverrides = new HashMap<String, Object>();
for (String envName : env.keySet()) {
  if (envName.contains("JDBC_DATABASE_URL")) {
    configOverrides.put("javax.persistence.jdbc.url", env.get(envName));
  }
}
entityManagerFactory = Persistence.createEntityManagerFactory("prod", configOverrides);

Here's a related example app. There are also many examples in the Heroku documentation including one for Spring XML config.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. To be fair this seems to be way too complicated for me and I just decided to use Spring Boot instead and I managed to deploy my app on Heroku. It worked, but now I have another problem stackoverflow.com/questions/50631388/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.