Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

MYSQL 8 - Quando inicio a app, as tabelas não sao criadas

Segui todos os passos mas não deu certo. Quando inicio a aplicação o console não mostra nenhum problema, entretanto as tabelas não são criadas no BD.

Por favor, podem me ajudar?

@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(vendorAdapter);

        //db connection
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("spedroza");
        dataSource.setPassword("1234");
        dataSource.setUrl("jdbc:mysql://localhost:3306/alura?useSSL=false");
        //dataSource.setDriverClassName("com.mysql.jdbc.Driver");

        //hibernate properties
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        factoryBean.setJpaProperties(properties);

        //models
        factoryBean.setPackagesToScan("br.com.spedroza.casadocodigo.loja.model");
        return factoryBean;
    }

mysql> use alura Database changed

mysql> show tables; Empty set (0.06 sec)

2 respostas

desculpe, notei agora um problema no log:

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception javax.servlet.ServletException: Servlet.init() for servlet [dispatcher] threw exception org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:409) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748)

Root Cause org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in br.com.spedroza.casadocodigo.loja.conf.JPAConfiguration: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.UnsupportedOperationException: The application must supply JDBC connections org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)

solução!

Consegui selecionar meu problema com 3 passos.

  1. Como estou usando Mysql 8, atualizei todas as dependencias no pom.xml
<!-- Hibernate dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
             <version>5.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
             <version>5.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
  1. Tinha esquecido de colocar o datasource no factorybean, isto solucionou o problema "The application must supply JDBC connections"
factoryBean.setDataSource(dataSource);
  1. Após isso, tive o problema "com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'E. South America Standard Tim' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver" Solucionei adicionando o timezone na url de conexao:
dataSource.setUrl("jdbc:mysql://localhost:3306/alura?useSSL=false&serverTimezone=UTC");

Agora funcionou.