Skip to content

KSP codegen: @QueryEntity with a @QuerySupertype base class fails with "Unable to resolve type of entity" (QueryModelType.autodetect doesn't know QuerySupertype) #1857

Description

@kylequera

Important Notice

Thank you for opening an issue! Please note that, as outlined in the README, I currently only work on feature requests or bug fixes when sponsored. Balancing this project with professional and personal priorities means I have a very limited amount of a effort I can divert to this project.

You must put in the work to address this issue, or it won't be addressed.

  • I am willing to put in the work and submit a PR to resolve this issue.

Describe the bug

QueryModelType.autodetect() in querydsl-ksp-codegen only recognizes jakarta.persistence.MappedSuperclass for SUPERCLASS models (and jakarta.persistence.Embeddable for EMBEDDABLE). When a @QueryEntity class has a superclass annotated with querydsl's own @QuerySupertype, autodetect returns null and the processor throws:

e: [ksp] java.lang.IllegalStateException: Unable to resolve type of entity for <superclass fqn>

No Q classes are generated. The failure is the same whether the @QuerySupertype base class is source in the same module or comes from a compiled library dependency.

To Reproduce

Generating Q classes for non-JPA (Spring Data R2DBC) entities using querydsl's own annotations:

import com.querydsl.core.annotations.QueryEntity
import com.querydsl.core.annotations.QuerySupertype
import java.time.LocalDateTime
import java.util.UUID

@QuerySupertype
abstract class AuditableEntity(
    open var id: UUID? = null,
    open var createdDate: LocalDateTime? = null,
)

@QueryEntity
class GroupEntity(
    id: UUID? = null,
    val name: String,
) : AuditableEntity(id)
dependencies {
    implementation("io.github.openfeign.querydsl:querydsl-core:7.4.0")
    ksp("io.github.openfeign.querydsl:querydsl-ksp-codegen:7.4.0")
}

Versions: querydsl-ksp-codegen:7.4.0 (affects the @QueryEntity support introduced in 7.1 via #1345), Kotlin 2.1.0, KSP 2.1.0-1.0.29 (KSP2), Gradle 8.10.2, JDK 21.

Expected behavior

KSP codegen should recognize @QuerySupertype and @QueryEmbeddable the same way the classic APT processor does, generate Q supertype classes for @QuerySupertype bases, and generate @QueryEntity subclasses with _super referencing the supertype Q class.

Root cause

QueryModelExtractor.addNew() walks the entity's superclass and calls the single-arg addClass(), which resolves the model type via QueryModelType.autodetect(). QueryModelType.SUPERCLASS.associatedAnnotations only contains jakarta.persistence.MappedSuperclass — querydsl's own com.querydsl.core.annotations.QuerySupertype was never added when @QueryEntity support landed (#1345), so autodetect returns null and addClass throws. EMBEDDABLE has the same gap for com.querydsl.core.annotations.QueryEmbeddable.

Proposed fix

In QueryModelType.kt, add the querydsl-native annotations to the associated annotation lists:

EMBEDDABLE(
    listOf(
        Embeddable::class.qualifiedName!!,
        "com.querydsl.core.annotations.QueryEmbeddable"
    )
),
SUPERCLASS(
    listOf(
        MappedSuperclass::class.qualifiedName!!,
        "com.querydsl.core.annotations.QuerySupertype"
    )
),

Additional context

I have validated this two-line change locally against 7.4.0 sources on a real two-build project (library @QuerySupertype base + consumer @QueryEntity entities): generation succeeds and the full integration-test suite passes.

A follow-up PR with the fix and regression tests is coming shortly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions