3
respostas

No persistence units parsed from {classpath*:META-INF/persistence.xml}

Pessoal, podem me ajudar na configuração deu um projeto Spring com JPA e Hibernate, por favor?

Comecei um projeto do zero, para treinar esta parte da persistência, e estou com erro ao tentar subir o TomCat.

Erro

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
fev 26, 2020 8:54:37 PM org.springframework.web.context.support.XmlWebApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
fev 26, 2020 8:54:37 PM org.springframework.web.servlet.DispatcherServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
    at 

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="peso">

        <!-- Implementação do JPA, no nosso caso Hibernate -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <!-- Aqui são listadas todas as entidades -->
        <class>br.com.moreira.peso.Peso</class>
        <class>br.com.moreira.peso.Usuario</class>
        <class>br.com.moreira.peso.Permissao</class>
        <class>br.com.moreira.peso.Pessoa</class>

        <properties>

            <!-- Configurações específicas do Hibernate -->
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>
3 respostas

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"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/tx 
                        http://www.springframework.org/schema/tx/spring-tx-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/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                           http://www.springframework.org/schema/data/jpa
                        http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd">

    <context:component-scan
        base-package="br.com.moreira.peso" />

    <jpa:repositories base-package="br.com.moreira.peso" />

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />

    <mvc:interceptors>
        <bean
            class="br.com.moreira.peso.interceptor.AutorizadorInterceptor" />
    </mvc:interceptors>

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

    <bean id="mysqlDataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc.mysql://localhost/controlepeso?serverTimezone=UTC" />
        <property name="username" value="root" />
        <property name="password" value="moreira89" />
    </bean>

    <!-- Gerenciamento de JPA pelo Spring -->
    <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>
<!-- gerenciamento da transação pelo spring -->
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory"
            ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />
</beans>

O seu persistence.xml está em src/main/resources/META-INF ?

No seu entityManagerFactory, coloque os packagesToScan e persistenceUnitName:

<property name="packagesToScan">
            <list>
                <value>br.com.moreira.peso</value>
            </list>
        </property>

<property name="persistenceUnitName" value="peso" />

O persistence.xml está em eclipse-workspace-moreira\peso\WebContent\META-INF.

Alterei o entityManagerFactory conforme abaixo:

    <!-- Gerenciamento de JPA pelo Spring -->
    <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>
        <property name="packagesToScan">
            <list>
                <value>br.com.moreira.peso</value>
            </list>
        </property>

        <property name="persistenceUnitName" value="peso" />
    </bean>

O erro mudou para

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-context.xml]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError: Receiver class org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4 does not define or inherit an implementation of the resolved method 'abstract java.util.Map getConfigurationValues()' of interface org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$Work.
fev 27, 2020 6:59:35 AM org.springframework.web.servlet.DispatcherServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-context.xml]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError: Receiver class org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4 does not define or inherit an implementation of the resolved method 'abstract java.util.Map getConfigurationValues()' of interface org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$Work.

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