UnknownEntityTypeException:无法找到持久性 [英] UnknownEntityTypeException: Unable to locate persister

查看:71
本文介绍了UnknownEntityTypeException:无法找到持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Wildfly上将JPA与Spring一起使用.我尝试了这种配置:

I want to use JPA with Spring on Wildfly. I tried this configuration:

application.properties:

application.properties:

spring.jmx.enabled=false
spring.datasource.jndi-name=java:/global/production
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.hibernate.ddl-auto = create-drop

POM文件:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>   
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>                
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>       
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

配置:

@Configuration
@ComponentScan("org.rest.api.server.*")
public class AppConfig {

    @Bean
    public EntityManager entityManager(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }
}

但是当我尝试执行查询时,我得到了:

But when I try to perform query I get:

Caused by: org.hibernate.UnknownEntityTypeException: Unable to locate persister: org.rest.api.server.repository.Terminals
10:28:27,539 ERROR [stderr] (default task-1)    at org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:642)

配置实体的正确方法是什么?也许我需要手动将其映射?

What is the proper way to configure Entity? probably I need to map it manually?

推荐答案

当未拾取实体,并且Spring Boot自动配置未创建或映射数据库表时,将引发此错误.为了解决这个问题,您需要添加在Spring Boot应用程序类中的 @SpringBootApplication 下方的 @EntityScan(basePackages = {"** entities_package_name"}} ).

This error is thrown When entities aren't being picked up, and database tables aren't being created or mapped by spring boot auto configuration. In order to solve this problem you need to add @EntityScan(basePackages = {"**entities_package_name"}) below @SpringBootApplication in Spring Boot application class.

package com.bill.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan( basePackages = {"com.bill.entity"} ) // entities package name
public class BillWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(BillWebApplication.class, args);
    }
}

这篇关于UnknownEntityTypeException:无法找到持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆