micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
466 stars 197 forks source link

.findById(Object id). Cannot query entity [X] on non-existent property: Id #2802

Closed transhkoot closed 8 months ago

transhkoot commented 8 months ago

Issue description

I'm trying to use the Spring Boot app as Micronaut. example here Everything work fine except when adding Spring jpa Data and trying to use database and jpa entities.

here is my gradle

`plugins { id("org.jetbrains.kotlin.jvm") version "1.9.22" id("org.jetbrains.kotlin.plugin.allopen") version "1.9.22" id("org.jetbrains.kotlin.plugin.jpa") version "1.9.22" id("com.google.devtools.ksp") version "1.9.22-1.0.17" id("com.github.johnrengelman.shadow") version "8.1.1" id("io.micronaut.application") version "4.3.2" id("io.micronaut.test-resources") version "4.3.2" id("io.micronaut.aot") version "4.3.2" }

version = "0.1" group = "com.example.spring"

val kotlinVersion=project.properties.get("kotlinVersion") repositories { mavenCentral() }

dependencies { ksp("io.micronaut.data:micronaut-data-processor") ksp("io.micronaut:micronaut-http-validation") ksp("io.micronaut.serde:micronaut-serde-processor") ksp("io.micronaut.spring:micronaut-spring-annotation") ksp("io.micronaut.spring:micronaut-spring-boot-annotation") ksp("io.micronaut.spring:micronaut-spring-web-annotation") implementation("io.micronaut:micronaut-http-server") implementation("io.micronaut.data:micronaut-data-hibernate-jpa") implementation("io.micronaut.data:micronaut-data-spring") implementation("io.micronaut.data:micronaut-data-spring-jpa") implementation("io.micronaut.kotlin:micronaut-kotlin-runtime") implementation("io.micronaut.serde:micronaut-serde-jackson") implementation("io.micronaut.sql:micronaut-jdbc-hikari") implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}") implementation("org.springframework:spring-orm") implementation("org.springframework.boot:spring-boot-starter") implementation("org.springframework.boot:spring-boot-starter-web") compileOnly("io.micronaut:micronaut-http-client") runtimeOnly("ch.qos.logback:logback-classic") runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin") runtimeOnly("io.micronaut.spring:micronaut-spring-boot") runtimeOnly("io.micronaut.spring:micronaut-spring-web") runtimeOnly("org.postgresql:postgresql") kspTest("io.micronaut.spring:micronaut-spring-boot-annotation") kspTest("io.micronaut.spring:micronaut-spring-web-annotation") testImplementation("io.micronaut:micronaut-http-client") }

application { mainClass.set("com.blockchain.Application") } java { sourceCompatibility = JavaVersion.toVersion("17") }

graalvmNative.toolchainDetection.set(false) micronaut { runtime("netty") testRuntime("junit5") processing { incremental(true) annotations("com.example.spring.*") } aot { // Please review carefully the optimizations enabled below // Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details optimizeServiceLoading.set(false) convertYamlToJava.set(false) precomputeOperations.set(true) cacheEnvironment.set(true) optimizeClassLoading.set(true) deduceEnvironment.set(true) optimizeNetty.set(true) } }

`

and here is the entity:

` @Entity public class MainEntity{ @Id Long id = 0L; Long timestamp;

} `

with empty repository

` import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;

@Repository public interface MainEntityRepository extends JpaRepository<MainEntity,Long> {} `

and I'm getting

Unable to implement Repository method: MainEntityRepository.findById(Object id). Cannot query entity [MainEntity] on non-existent property: Id

I tried it with kotlin too it's the same error. Is it a bug?

dstepanov commented 8 months ago

Please show your project

transhkoot commented 8 months ago

Please show your project

Like what? I cloned this project and added Repository and Entity I also added dependencies to gradle . Would you like me to upload the project ?

dstepanov commented 8 months ago

yes please

transhkoot commented 8 months ago

yes please

Sure. Here it is micronaut-spring-boot-gradle-java_.zip

radovanradic commented 8 months ago

The entity is missing public getters and setters, that is why this error is reported. However, when that error is fixed, the new error pops up as it seems we don't support Spring JpaRepository. Changing it to CrudRepository should work.

transhkoot commented 8 months ago

I edited the project as you mentioned but I got some errors about test and websocket (I'm not sure why) After that I tried to call the repository but unfortunately. I got another error Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [io.micronaut.data.operations.PrimaryRepositoryOperations] exists. This error appears when I make the first call for any of repository's method

radovanradic commented 8 months ago

I have updated your project a little bit and added repo tests to the DefaultTest. Here is attached updated project and repo tests are working, hope it will help micronaut-spring-boot-gradle-java-updated.zip

transhkoot commented 8 months ago

Thank you so much it works . I'm gonna close the issue as it has been solved