Olá,
To com um problema para subir o banco de dados da aplicação! Criei um classe que verificar se um usuário existe, caso não exista ele cria este usuário assim que inicia a aplicação. Em modo local ele funcionou, mas assim que subi par ao heroku ele deu problema, e em uma das linha que ele tenta verificar o role diz que não existe, além disso ao acessar as tabelas do heroku ele mostra que a aplicação não criou as tabelas no postegresql e não consegue acessar.
JPAConfiguration
@Configuration
@EnableTransactionManagement
public class JPAConfiguration {
@Bean
@Profile("dev")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,Properties additionalProperties) {
LocalContainerEntityManagerFactoryBean entityManagerFactory =
new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
entityManagerFactory.setJpaProperties(additionalProperties);
return entityManagerFactory;
}
@Bean
@Profile(value="dev")
public Properties additionalProperties() {
Properties additionalProperties = new Properties(); additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
return additionalProperties;
}
@Autowired
private Environment env;
}
JPAConfigurationProduction
@Profile(value="prod")
public class JPAProductionConfiguration {
@Autowired
private Environment environment;
@Bean
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
public DataSource dataSource() throws URISyntaxException {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
// usuario:senha@host:port/path
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;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
PROCFILE
web: java -Dserver.port=$PORT -Dspring.profiles.active=prod -jar target/*.jar