1
resposta

Problema com o @PersistenceContext EntityManager

Resumindo, eu estava fazendo um projeto web crud normalmente sem erros criando o meu proprio entity manager com begin e commit, porém depois que comecei a usar a notação @PersistenceContext no Entity Manager comecei a ter problemas, aí vi que eu teria que adicionar uma bean entityManagerFactory no xml, e assim o fiz, mas não adiantou muita coisa o erro seguinte que veio foi este: Então estou achando que o problema é de configuração... Minha classe JpaDao está com a notação @Repository e @PersistenceContext no EntityManager

Este é o erro

Exception

javax.servlet.ServletException: Servlet.init() for servlet [Spring MVC Dispatcher Servlet] threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)



Root Cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'menuController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: br.com.colibri.dao.AnotacoesJpaDao br.com.colibri.controller.MenuController.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotacoesJpaDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-context.xml]: Cannot create inner bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#5c0f3a75' of type [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter] while setting bean property 'jpaVendorAdapter'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#5c0f3a75' defined in ServletContext resource [/WEB-INF/spring-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)

Esta é a configuração do meu spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <context:component-scan base-package="br.com.colibri" />
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>


<bean class="org.apache.commons.dbcp.BasicDataSource" id="mysqlDataSource">

<property value="com.mysql.jdbc.Driver" name="driverClassName"/>

<property value="jdbc:mysql://localhost/colibri?useTimezone=true&serverTimezone=UTC" name="url"/>

<property value="root" name="username"/>

<property value="password" name="password"/>

</bean>
<bean id="entityManagerFactory" 
        class=
            "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="mysqlDataSource" />
    <property name="jpaVendorAdapter">
        <bean 
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
</bean>

<tx:annotation-driven/>

</beans>
1 resposta

Matheus esse projeto está no github ?

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software