UnknownEntityTypeException:无法找到持久器

时间:2018-07-25 07:34:33

标签: java spring jpa spring-data-jpa

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

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();
    }
}

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

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)

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

4 个答案:

答案 0 :(得分:2)

当未通过Spring Boot自动配置拾取实体并且未创建或映射数据库表时,抛出此错误。

要解决此问题,您需要添加

在春季启动应用程序类的@SpringBootApplication下面的

@EntityScan(basePackages = {“ ** entities_package_name”})。

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);

    }

}

答案 1 :(得分:1)

这就是我配置entityManager的方式。您传递的dataSource是配置中的另一个bean。您可以看一下BasicDataSource。

Option Explicit

Sub PPR()

    Dim wb As Workbook
    Dim tgt As Worksheet
    Dim stt As Worksheet
    Set wb = ThisWorkbook
    Set tgt = wb.Sheets("Sheet1")
    Set stt = wb.Sheets("Sheet2")
    tgt.Cells.ClearContents
    Dim pID As String
    Dim sType As String
    Dim sDateInt As String
    Dim sHourInt As String
    Dim sMinuteInt As String
    Dim eDateInt As String
    Dim eHourInt As String
    Dim eMinuteInt As String
    Dim url As String
    Dim url1 As String
    Dim url2 As String
    Dim url3 As String
    Dim url4 As String
    Dim url5 As String
    Dim url6 As String
    Dim url7 As String
    Dim url8 As String

'Variables
    pID = stt.Range("B1")
    sType = stt.Range("B2")
    sDateInt = Format(Now, "YYYY/MM/DD")
    sHourInt = stt.Range("B4")
    sMinuteInt = stt.Range("C4")
    eDateInt = Format(Now, "YYYY/MM/DD")
    eHourInt = stt.Range("B5")
    eMinuteInt = stt.Range("C5")

    url1 = "URL;https://blabla.com/reports/blabla?reportFormat=CSV&blabla=bla"
    url2 = url1 & "&blabla=" & pID
    url3 = url2 & "&maxIntradayDays=1&spanType=" & sType
    url4 = url3 & "&startDateIntraday=" & sDateInt
    url5 = url4 & "&startHourIntraday=" & sHourInt
    url6 = url5 & "&startMinuteIntraday=" & sMinuteInt
    url7 = url6 & "&endDateIntraday=" & eDateInt
    url8 = url7 & "&endHourIntraday=" & eHourInt
    url = url8 & "&endMinuteIntraday=" & eMinuteInt

    With tgt.QueryTables.Add(Connection:= _
        url, _
        Destination:=tgt.Range("A1"))
        .Refresh
    End With

    tgt.Activate
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
    Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
    ), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1)), _
    TrailingMinusNumbers:=True


End Sub

答案 2 :(得分:1)

您可以使用@EntityScan()批注来让spring查找实体类。用法类似于@ComponentScan

@EntityScan docs

答案 3 :(得分:0)

当您的persistence.xml文件缺少您尝试使用的某些实体时,可能会发生此错误。