2
respostas

Erro ao publicar o projeto no Heroku

Olá,

A minha aplicação está 100% quando executo localmente utilizando o mysql, mas quando faço o deploy da aplicação no Heroku utilizando o postgresql da erro no momento de criar a tabela de produtos.

Erro: 2019-11-30T18:56:41.243911+00:00 app[web.1]: at java.lang.Thread.run(Thread.java:748) 2019-11-30T18:56:41.244210+00:00 app[web.1]: Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet 2019-11-30T18:56:41.244251+00:00 app[web.1]: at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123) 2019-11-30T18:56:41.244289+00:00 app[web.1]: at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 2019-11-30T18:56:41.244325+00:00 app[web.1]: at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) 2019-11-30T18:56:41.244363+00:00 app[web.1]: at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) 2019-11-30T18:56:41.244427+00:00 app[web.1]: at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89) 2019-11-30T18:56:41.244457+00:00 app[web.1]: at org.hibernate.loader.Loader.getResultSet(Loader.java:2065) 2019-11-30T18:56:41.244492+00:00 app[web.1]: at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862) 2019-11-30T18:56:41.244525+00:00 app[web.1]: at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838) 2019-11-30T18:56:41.244558+00:00 app[web.1]: at org.hibernate.loader.Loader.doQuery(Loader.java:909) 2019-11-30T18:56:41.244593+00:00 app[web.1]: at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) 2019-11-30T18:56:41.244673+00:00 app[web.1]: at org.hibernate.loader.Loader.doList(Loader.java:2551) 2019-11-30T18:56:41.244703+00:00 app[web.1]: at org.hibernate.loader.Loader.doList(Loader.java:2537) 2019-11-30T18:56:41.244739+00:00 app[web.1]: at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367) 2019-11-30T18:56:41.244773+00:00 app[web.1]: at org.hibernate.loader.Loader.list(Loader.java:2362) 2019-11-30T18:56:41.244822+00:00 app[web.1]: at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496) 2019-11-30T18:56:41.244856+00:00 app[web.1]: at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387) 2019-11-30T18:56:41.244889+00:00 app[web.1]: at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229) 2019-11-30T18:56:41.244922+00:00 app[web.1]: at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260) 2019-11-30T18:56:41.244975+00:00 app[web.1]: at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) 2019-11-30T18:56:41.245010+00:00 app[web.1]: at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573) 2019-11-30T18:56:41.245055+00:00 app[web.1]: at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449) 2019-11-30T18:56:41.245109+00:00 app[web.1]: ... 102 more 2019-11-30T18:56:41.245665+00:00 app[web.1]: Caused by: org.postgresql.util.PSQLException: ERROR: relation "produto" does not exist 2019-11-30T18:56:41.245711+00:00 app[web.1]: Position: 330

Segue meu código do github: https://github.com/itarocks/GetInfoMaximo

Alguem teve esse problema?

Abraços

2 respostas

Fala amigo,

seu problema está provavelmente na classe JPAConfiguration. Lembra que você estava rodando local no mySQL e no Heroku está rodando no PostregreSQL?

Tem que atualizar as propriedades para o PostgreSQL alterando o código como abaixo:

    @Bean
    @Profile("dev")
    public Properties additionalProperties() {
        Properties props = new Properties();
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        props.setProperty("hibernate.show_sql", "true");
        props.setProperty("hibernate.hbm2ddl.auto", "update");
        return props;
    }

    @Bean
    @Profile("dev")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("AdminSemSenha");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/financas");

        dataSource.setDriverClassName("org.postgresql.Driver");
        return dataSource;
    }

Espero ter ajudado

Olá Thiago Augusto,

Tudo bem?

Muito obrigado pela ajuda.  Então no meu projeto eu criei uma classe exclusiva com as configurações de prod: 

JPAProductionConfiguration.java

package br.com.casadocodigo.loja.conf;

import java.net.URI; import java.net.URISyntaxException; import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Profile; import org.springframework.core.env.Environment; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement @Profile("prod") public class JPAProductionConfiguration {

@Autowired
private Environment environment;

@Bean
public Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    properties.setProperty("hibernate.show_sql", "true");
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    return properties;
}

@Bean
private DriverManagerDataSource dataSource() throws URISyntaxException {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");

    URI dbUrl = new URI(environment.getProperty("DATABASE_URL"));

    dataSource.setUrl("jdbc:postgresql://"+dbUrl.getHost()+":"+dbUrl.getPort()+dbUrl.getPath());
    dataSource.setUsername(dbUrl.getUserInfo().split(":")[0]);
    dataSource.setPassword(dbUrl.getUserInfo().split(":")[1]);

    return dataSource;
}

}

Depois disso comentei o onstartup da Servlet Spring:

/*@Override public void onStartup(ServletContext servletContext) throws ServletException { // TODO Auto-generated method stub super.onStartup(servletContext); servletContext.addListener(RequestContextListener.class); servletContext.setInitParameter("spring.profiles.active", "dev"); } */ E mesmo assim não cria a tabela de produtos. Acho que vou instalar o postgree em minha maquina.

Obrigado