I am currently testing Kotlin and Gradle for a small API. I want the API to be connected to a local MySQL database and use GraphQL. I am running into a problem right now, which is when I start the application it says java.lang.IllegalArgumentException: Not a managed type: class de.wi2020sebgroup1.nachhilfe.gamification.Stats
I've tried using @EntityScan and @ComponentScan, tried refactoring the packages and I added @javax.persistence.Entity to my Entity. Still, the error persists. Can anyone find something that I am missing?
All the following .kt files lie in the same package
Error Log:
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()
}
Entity that is not a managed type:
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
)
Main App:
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 Repo
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 Resolver
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
}
}
Have a nice weekend! :)
CodePudding user response:
Spring 3.0 works with Jakarta EE 9 instead of Javax EE 8. In order to get the application running I had to include implementation("jakarta.platform:jakarta.jakartaee-web-api:9.0.0")
in my build.gradle.kt
instead of javax. After changing the imports from javax.persistence
to jakarta.persistence
it worked fine :)
CodePudding user response:
Change the imports statement from javax.persistence.xyz to jakarta.persistence.xyx. Springboot 3.0.0 doesn't support javax.persistence and recommends jakarta.persistence which works fine.