Java Spring:缺少 JPA 元模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26736241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:11:46  来源:igfitidea点击:

Spring: Missing JPA Metamodel

javaspringhibernate

提问by Maksim

I cannot understand what is going wrong with my simple Spring MVC project with JPA repositories. Could you please give a hint.

我不明白我的带有 JPA 存储库的简单 Spring MVC 项目出了什么问题。你能不能给个提示。

Domain:

:

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...

}

}

Repository

存储库

package com.test.app;

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

Controller

控制器

@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

<?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

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

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

</beans>

Exception

例外

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!

回答by Maksim

Xstianis right. I've lost entityManagerFactory declaration. Here is a sample that is working for me now:

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>

回答by Mahesh

I faced this issue, when I was using Hibernate 4 (SessionFactory for persistence way, instead of EntityManagers) with Spring Boot. Adding this got rid of the error. Perhaps helps someone.

当我在 Spring Boot 中使用 Hibernate 4(用于持久性方式的 SessionFactory,而不是 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>