복붙노트

[SPRING] 봄 : 누락 된 JPA 메타 모델

SPRING

봄 : 누락 된 JPA 메타 모델

JPA 리파지토리를 사용하는 간단한 스프링 MVC 프로젝트에서 무엇이 잘못 될지 이해할 수 없습니다. 힌트를 주시겠습니까?

도메인:

package com.test.app;

@Entity
@Table(name = "foo_table")
public class FooDomain {

    @Id
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;

    @Column(name = "text", nullable = false)
    private String text;

    // getters & setters here...

}

저장소

package com.test.app;

@RepositoryDefinition(domainClass=FooDomain.class, idClass=Long.class)
public interface FooRepository extends CrudRepository<FooDomain, Long> {}

제어 장치

@Controller
public class HomeController {

    @Autowired
    private FooRepository fooRepository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        model.addAttribute("rowsNumber", fooRepository.count());
        return "home";
    }

}

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns & xsi here...>
    <context:annotation-config />

    <!-- Defining folders containing bean components (@Component, @Service)  -->
    <context:component-scan base-package="ru.lexikos.app" />

   <import resource="hibernate.xml" />

   <import resource="repositories.xml" />

   <context:component-scan base-package="com.test.app" />
</beans>

hibernate.xml

<?xml xmlns & xsi here...>

    <context:property-placeholder location="classpath:db-connection.properties" />

    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.pass}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            </props>
        </property>
    </bean>

</beans>

repositories.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns & xsi here...>

  <jpa:repositories base-package="com.test.app"/>

</beans>

예외

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMapppingContext': Invocation of init method failed; nested exception is ja
va.lang.IllegalArgumentException: At least one JPA metamodel must be present!
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

해결법

  1. ==============================

    1.Xstian이 맞습니다. entityManagerFactory 선언을 잃어 버렸습니다. 지금 나를 위해 일하는 샘플은 다음과 같습니다.

    Xstian이 맞습니다. entityManagerFactory 선언을 잃어 버렸습니다. 지금 나를 위해 일하는 샘플은 다음과 같습니다.

    hibernate.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jpa="http://www.springframework.org/schema/data/jpa"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
        <context:property-placeholder location="classpath:db-connection.properties" />
    
        <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.user}" />
            <property name="password" value="${jdbc.pass}" />
        </bean>
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                </props>
            </property>
        </bean>
    
        <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="true"/>
            <property name="database" value="MYSQL"/>
        </bean>
    
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
            <!-- spring based scanning for entity classes>-->
            <property name="packagesToScan" value="com.test.app"/>
        </bean>
    
        <!-- Enables the Hibernate @Transactional programming model -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <jpa:repositories base-package="com.test.app"/>
    
    </beans>
    
  2. ==============================

    2.Spring 부트로 Hibernate 4 (SessionFactory for EntityManagers 대신)를 사용할 때이 문제에 직면했다. 이것을 추가하면 오류가 제거됩니다. 누군가를 돕는 것 같습니다.

    Spring 부트로 Hibernate 4 (SessionFactory for EntityManagers 대신)를 사용할 때이 문제에 직면했다. 이것을 추가하면 오류가 제거됩니다. 누군가를 돕는 것 같습니다.

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.0.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>ejb3-persistence</artifactId>
                    </exclusion>
                    <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-annotations</artifactId>
            </exclusion>
        </exclusions>               
    </dependency>
    
  3. from https://stackoverflow.com/questions/26736241/spring-missing-jpa-metamodel by cc-by-sa and MIT license