HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
Boa tarde, Leandro! Como vai?
Vc está obtendo um NullPointerException
pq não foi possível criar uma EntityManager
.
Vc poderia colar aqui o código que está sendo executada quando dá esse erro?
Oi Leandro, tudo bem ?
Cara você tá levando um nullpointer ai..
Consegue confirmar se está executanto todos os passos ?
CONFIGURANDO SPRING
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//ABILITANDO A TRANSAÇÃO PARA O SPRING TOMA CONTA
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");
factoryBean.setJtaDataSource(dataSource);
// DIALECT QUE IREMOS USAR EM NOSSA APLICAÇÃO
Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
props.setProperty("hibernate.show_sql", "true");// VISUALIZANDO NOSSO SQL
props.setProperty("hibernate.hbm2ddl.auto", "update");// MAPEAMENTO DO A CADA ALTERAÇÃO O HIBERNATE DA UM UPDATE AUTOMÁTICO NO BANCO
factoryBean.setJpaProperties(props);
factoryBean.setPackagesToScan("br.com.casadocodigo.loja.models");// ONDE ELE VAI ENCONTRA AS PROPROENDKDAKDKADKDJ
return factoryBean;// SE RECLAMAR DA UM CTRL + 1 -- SERÁ SUBSTITUIDO O VOID QUE NAO RETORNA NADA
}
@Bean
public JpaTransactionManager TransactionManager(EntityManagerFactory emf){// FAZENDO TRANSAÇÃO
return new JpaTransactionManager(emf);
}
}
fiz a chamada direitinho JPAConfiguration.class no protected Class
package br.com.casadocodigo.loja.conf;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
// CTRL +1 -- PARA FAZER A IMPORT
// CRTL + ESPAÇO -- PARA DA UM AUTO COMPLITER
public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer{
// CONFIGURANDO SPRING PELO CODIGO JAVA MELHOR OPÇÃO OU SE DESEJA CONFIGURA PELO XML
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {AppWebConfiguracao.class, JPAConfiguration.class};// CLASSE DE CONFIGURAÇÃO DE PROJETO WEB QUE IREMOS UTILIZAR
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};// INFORMANDO QUAL URL O SPRING DEVE MAPEAR NO CASO / EM DIANTE O SPRING TOMARÁ CONTA DO MAPEAMENTO
}
}
esse erro acontece quando configura essas duas classes acima e a public class ProdutoDAO.
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import br.com.casadocodigo.loja.models.Produto;
// JPAConfiguration
@Repository//FAZENDO COM QUE O SPRING RECONHEÇA O PRODUTO DAO E MAPEA-LO
@Transactional
public class ProdutoDAO {
@PersistenceContext
private EntityManager manager;
public void gravar(Produto produto){
manager.persist(produto);
}
}
erro ao executar aplicação
HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
type Exception report
message Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
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.NullPointerException
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:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
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.loja.daos.ProdutoDAO$$EnhancerBySpringCGLIB$$b789cace.gravar(<generated>)
br.com.casadocodigo.loja.controllers.ProdutosController.gravar(ProdutosController.java:25)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
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)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
preciso da ajuda de vocês