java.lang.IllegalArgumentException:不是托管类型:@Entity

问题描述 投票:0回答:3

我目前正在测试 Kotlin 和 Gradle 的小型 API。我希望 API 连接到本地 MySQL 数据库并使用 GraphQL。我现在遇到一个问题,当我启动应用程序时,它显示

java.lang.IllegalArgumentException: Not a managed type: class de.wi2020sebgroup1.nachhilfe.gamification.Stats

我尝试使用@EntityScan和@ComponentScan,尝试重构包并将@javax.persistence.Entity添加到我的实体中。尽管如此,错误仍然存在。谁能找到我丢失的东西吗?

以下所有 .kt 文件都位于同一个包中

错误日志:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'statsResolver': Unsatisfied dependency expressed through field 'repo': Error creating bean with name 'statsRepository' defined in de.wi2020sebgroup1.nachhilfe.gamification.StatsRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: 
Not a managed type: class de.wi2020sebgroup1.nachhilfe.gamification.Stats

[...]

Caused by: java.lang.IllegalArgumentException: Not a managed type: class de.wi2020sebgroup1.nachhilfe.gamification.Stats
        at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:181) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
        at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:496) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
        at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:99) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
        at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:77) ~[spring-data-jpa-3.0.0.jar:3.0.0]
        at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:69) ~[spring-data-jpa-3.0.0.jar:3.0.0]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:246) ~[spring-data-jpa-3.0.0.jar:3.0.0]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:211) ~[spring-data-jpa-3.0.0.jar:3.0.0]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:194) ~[spring-data-jpa-3.0.0.jar:3.0.0]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) ~[spring-data-jpa-3.0.0.jar:3.0.0]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317) ~[spring-data-commons-3.0.0.jar:3.0.0]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279) ~[spring-data-commons-3.0.0.jar:3.0.0]  
        at org.springframework.data.util.Lazy.getNullable(Lazy.java:229) ~[spring-data-commons-3.0.0.jar:3.0.0]
        at org.springframework.data.util.Lazy.get(Lazy.java:113) ~[spring-data-commons-3.0.0.jar:3.0.0]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285) ~[spring-data-commons-3.0.0.jar:3.0.0]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132) ~[spring-data-jpa-3.0.0.jar:3.0.0]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[spring-beans-6.0.2.jar:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[spring-beans-6.0.2.jar:6.0.2]
        ... 35 common frames omitted

build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.0.0"
    id("io.spring.dependency-management") version "1.1.0"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.7.21"
    kotlin("jvm") version "1.7.21"
    kotlin("plugin.spring") version "1.7.21"
    kotlin("plugin.jpa") version "1.7.21"
}

allOpen {
    annotation("javax.persistence.Entity")
    annotation("org.springframework.stereotype.Repository")
    annotation("org.springframework.stereotype.Component")
}

group = "de.wi2020sebgroup1.nachhilfe"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
    mavenCentral()
}

dependencies {
    implementation("javax:javaee-api:8.0")

    implementation("com.graphql-java:graphql-spring-boot-starter:5.0.2")
    implementation("com.graphql-java:graphiql-spring-boot-starter:5.0.2")
    implementation("com.graphql-java:voyager-spring-boot-starter:5.0.2")
    implementation("com.graphql-java:graphql-java-tools:5.2.4")

    implementation("org.springframework.data:spring-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-graphql")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")

    implementation("org.hibernate:hibernate-core:6.1.5.Final")

    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")

    runtimeOnly("com.mysql:mysql-connector-j")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework:spring-webflux")
    testImplementation("org.springframework.graphql:spring-graphql-test")
    testImplementation("org.hibernate:hibernate-testing:6.1.5.Final")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

非托管类型的实体:

package de.wi2020sebgroup1.nachhilfe.gamification

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Entity
@Table(name="stats")
class Stats( 

    val userId: String, 
    val registerDate: String,
    val learningPoints: Int,
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: String

)

主要应用程序:

package de.wi2020sebgroup1.nachhilfe.gamification

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.runApplication
import org.springframework.data.jpa.repository.config.EnableJpaRepositories

@SpringBootApplication
class GamificationApplication

fun main(args: Array<String>) {
    runApplication<GamificationApplication>(*args)
}

JPA 仓库

package de.wi2020sebgroup1.nachhilfe.gamification

import org.springframework.stereotype.Repository
import org.springframework.data.jpa.repository.JpaRepository
import com.coxautodev.graphql.tools.GraphQLQueryResolver
import de.wi2020sebgroup1.nachhilfe.gamification.Stats

@Repository
interface StatsRepository: JpaRepository < Stats, String > {
    fun findByuserId(userId: String): MutableList < Stats >
    fun findByid(id: String): MutableList < Stats >
}

GraphQL 解析器

package de.wi2020sebgroup1.nachhilfe.gamification

import org.springframework.stereotype.Component
import org.springframework.beans.factory.annotation.Autowired
import com.coxautodev.graphql.tools.GraphQLQueryResolver
import de.wi2020sebgroup1.nachhilfe.gamification.Stats
import de.wi2020sebgroup1.nachhilfe.gamification.StatsRepository

@Component
class StatsResolver() : GraphQLQueryResolver {

    @Autowired
    lateinit var repo: StatsRepository

    fun stats() = repo.findAll()
    fun stat(id: String) = repo.findByid(id)
    fun statByUser(userId: String) = repo.findByuserId(userId)
    fun add(stats: Stats): Stats {
        repo.save(stats)
        return stats
    }
}

周末愉快! :)

spring spring-boot kotlin gradle spring-data-jpa
3个回答
3
投票

Spring 3.0 使用 Jakarta EE 9 而不是 Javax EE 8。为了让应用程序运行,我必须在我的

implementation("jakarta.platform:jakarta.jakartaee-web-api:9.0.0")
中包含
build.gradle.kt
而不是 javax。将导入从
javax.persistence
更改为
jakarta.persistence
后,效果很好:)


0
投票

将导入语句从 javax.persistence.xyz 更改为 jakarta.persistence.xyx。 Springboot 3.0.0不支持javax.persistence,推荐使用jakarta.persistence,效果很好。


0
投票

谢谢!将 javax.persistence.* 更改为 jakarta.persistence.* 有效

© www.soinside.com 2019 - 2024. All rights reserved.