Error while booting application with Spring Boot

Asked

Viewed 2,434 times

0

I am creating an application using Spring Boot with JPA. When I go up the application, the following message is launched:

Description: Cannot determine Embedded database driver class for database type NONE Action: If you want an Embedded database Please put a supported one on the classpath. >If you have database Settings to be Loaded from a particular profile you may >need to active it (no profiles are Currently active).

Okay, I googled and found a topic from Stackoverflow himself (reply link here), talking to set up the application.properties which DataSource will be used. However, I do not want to do directly in this file, because I want to set the DataSource directly in the application.

My class:

@Configuration
@EnableJpaRepositories("br.com.sys.dao")
@EnableTransactionManagement
public class JpaConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        DataSource ds = dataSource();

        ConnectionManager.getInstance().setDataSource(ds);

        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "validate");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.format_sql", "false");
        properties.setProperty("hibernate.use_sql_comments", "false");
        properties.setProperty("hibernate.physical_naming_strategy", "br.com.sys.configuration.LowercaseNamingStrategy");

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(ds);
        em.setPackagesToScan("br.com.sys.model");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        em.setJpaProperties(properties);

        return em;
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/bd");
        dataSource.setUsername("postgres");
        dataSource.setPassword("postgres");
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}

The annotation @Configuration should not be "calling" Springboot to this class in order to make the configuration of DataSource? The way I’m doing it is right, or should use the application.properties?

Someone????

2 answers

0


The problem was because my main class, which contains Bootapplication, by my carelessness, was not in the root package of the project. Therefore, he could not read my configuration class posted above. After moving my main class to the root package, everything worked perfectly.

0

Hello, I think it’s almost all right, but whenever I want to apply Hibernate settings I call directly the setHibernateProperties method in the creation of Factory. Follow my example:

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.websystique.spring.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
 }

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    return properties;        
}
  • Man, I’m sorry, I just forgot to answer my own question. The problem dates because my main class, which contains Bootapplication, by my carelessness, was not in the root package of the project. Therefore, he could not read my configuration class posted above. But thank you for your reply.

  • @Cristianobombazar, could you answer your question and mark it as correct? That way other people will know that it’s been solved and how did you solve the problem

  • 1

    @Denisrudneidesouza Feito. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.