5
respostas

Erro ao criar o Entity manager

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.UnsupportedOperationException: The application must supply JDBC connections
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause

org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.UnsupportedOperationException: The application must supply JDBC connections
    org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:430)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
    org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:438)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:261)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    br.com.casadocodigo.lojas.daos.ProdutoDao$$EnhancerBySpringCGLIB$$1342d036.gravar(<generated>)
    br.com.casadocodigo.lojas.controllers.ProdutosController.grava(ProdutosController.java:23)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
5 respostas

Opa Bruno, acho que está faltando uma parte da exception, mas lendo o que está aí, meu chute é que faltou você configurar o datasource.

package br.com.casadocodigo.loja.conf;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;

import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement public class JPAConfiguration { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

factoryBean.setJpaVendorAdapter(vendorAdapter);

DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUsername("root"); dataSource.setPassword(""); dataSource.setUrl("jdbc:mysql://localhost:3306/casadocodigo"); dataSource.setDriverClassName("com.mysql.jdbc.Driver");

Properties props = new Properties(); props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); props.setProperty("hibernate.show_sql", "true"); props.setProperty("hibernate.hbm2ddl.auto", "update");

factoryBean.setJpaProperties(props); factoryBean.setPackagesToScan("br.com.casadocodigo.model");

return factoryBean; }

@Bean public JpaTransactionManager transactionManager(EntityManagerFactory emf) { return new JpaTransactionManager(emf);

} }

esse ai é a classe de configuração. Segui todos os passos da aula e tá dando esse erro

package br.com.casadocodigo.lojas.daos; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import br.com.casadocodigo.model.Produto;

@Repository @Transactional public class ProdutoDao {

@PersistenceContext private EntityManager manager;

public void gravar(Produto produto) { manager.persist(produto); }

}

Tens o mysql rodando de acordo com essas configurações? Por aqui eu havia colocado senha para o root e me esquecido de mexer na configuração. Outro detalhe, nas tuas dependências, está o conector correspondente ao Mysql? Vale a pena ver se o Maven conseguiu fazer o download da versão mais recente. Dá uma olhada na criação do schema tb, se o nome está igual ao da configuração. Pelo erro ali, o projeto não está conseguindo se conectar ao MySql. Good Luck