Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin support #1828

Closed
sdeleuze opened this issue Mar 20, 2016 · 43 comments · Fixed by #2794
Closed

Kotlin support #1828

sdeleuze opened this issue Mar 20, 2016 · 43 comments · Fixed by #2794
Labels
Milestone

Comments

@sdeleuze
Copy link

Kotlin is pretty popular these days, and it would be nice to have Kotlin support in Query DSL, in addition to the Scala one. It could allow me to use Query DSL SQL for the use case I have detail on https://spring.io/blog/2016/03/20/a-geospatial-messenger-with-kotlin-spring-boot-and-postgresql.

@robi42
Copy link

robi42 commented Mar 20, 2016

+1

4 similar comments
@mackristof
Copy link

+1

@dustins
Copy link

dustins commented Apr 7, 2016

+1

@maurofran
Copy link

+1

@worwornf
Copy link

+1

@aldo-gutierrez-tierconnect-com

+1

@Shredder121
Copy link
Member

So, I was looking a bit, and the reference is a good start, but because we are not the intended audience for that[1] we'll have to consult the language spec.

I unfortunately have to say that there is not much to work with here.
The language spec is not complete, which isn't that much of a problem, but since we'll integrate with the tooling we will want to depend on laid out infrastructure, not kapt 0.1-SNAPSHOT.

It's of course a little overkill to require Java level of detail documentation, but a bit less TODOs might go a long way.

We will however add support for Kotlin to Codegen, so if at one point there is more information about how to integrate nicely, we can add such an integration.

So with that in mind, I think we'll keep this issue open until that moment.

[1]:This reference is designed for you to easily learn Kotlin in a matter of hours.

@sdeleuze
Copy link
Author

I will reach Kotlin team about the problems you raised and make you a feedback.

@DmRomantsov
Copy link

is there any news?

@Shredder121
Copy link
Member

There are still some issues.

You can also join in the Kotlin Slack, @sdeleuze already forwarded the request and they heard our request.

Unfortunately, there is still not much movement on the spec.

@raderio
Copy link

raderio commented Jun 30, 2016

+1

4 similar comments
@debop
Copy link

debop commented Sep 29, 2016

+1

@owahlen
Copy link

owahlen commented Oct 24, 2016

+1

@mdomasevicius
Copy link

+1

@kucharzyk
Copy link

+1

@Shredder121
Copy link
Member

This is an upstream issue, please refrain from posting +1 comments.
You can use the new reactions to show your involvement in the issue.

N.B. it looks like they moved the specification documentation into the main kotlin repository.
https://github.com/JetBrains/kotlin/tree/master/spec-docs

@mrfint
Copy link

mrfint commented Jan 3, 2017

+1

@owahlen
Copy link

owahlen commented Jan 3, 2017

Note that with the current version of Kotlin it is now possible to generate the Q-classes with kapt.
A build.gradle has to contain the following parts:

...
dependencies {
    
    kapt "com.querydsl:querydsl-apt:$querydsl_version:jpa"
    kapt 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'
    ...
    compile "com.querydsl:querydsl-jpa:$querydsl_version"
}

kapt {
    generateStubs = true
}

sourceSets {
    main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
}

@sdeleuze
Copy link
Author

sdeleuze commented Jan 4, 2017

You should also try the new kotlin-kapt plugin provided with Kotlin 1.0.6, see https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-0-6-is-here/. Not a silver bullet yet but seems promising.

@zvozin
Copy link

zvozin commented Jul 21, 2017

@Shredder121
Copy link
Member

Shredder121 commented Jul 21, 2017 via email

@zvozin
Copy link

zvozin commented Jul 21, 2017

For your convenience, I just took it apart and rebuilt on latest Kotlin, Hibernate 5.1.8.Final and QueryDSL 3.7.4. Relevant bits of build.gradle: https://gist.github.com/zvozin/f46cc9fe1dd00bb648808317e77bf8ca.

@geertolaerts
Copy link

Any example on how to do it with maven. I tried to get it done, but failed miserably :(

@Adeynack
Copy link

The examples on this issue + some tweaking to be more up-to-date allowed me to make it work.
Here are the additions to my build.gradle file to make it work.

buildscript {
  // ...
  ext {
    queryDslVersion = '4.1.3'
  }
}

// ...

apply plugin: 'idea'
apply plugin: 'kotlin-kapt'

dependencies {
    compile "com.querydsl:querydsl-jpa:$queryDslVersion"
    kapt "com.querydsl:querydsl-apt:$queryDslVersion:jpa"
}
idea {
    module {
        def kaptMain = file('build/generated/source/kapt/main')
        sourceDirs += kaptMain
        generatedSourceDirs += kaptMain
    }
}

@nhajratw
Copy link

FWIW, building off @Adeynack -- here's what I did to get it working fine with command line & intellij

plugins {
// ...
    id "org.jetbrains.kotlin.kapt" version "1.2.70"
}

// ...
dependencies {
    compile "com.querydsl:querydsl-jpa:4.2.1"
    kapt "com.querydsl:querydsl-apt:4.2.1:jpa"
}

I didn't need the additional idea section

@zzlhr
Copy link

zzlhr commented Oct 24, 2018

How to configure maven, my project can compile if the service layer does not create QModel, but can't compile when the service layer creates QModel

@mykola-dev
Copy link

mykola-dev commented Nov 6, 2018

Doesn't work with Kotlin 1.3
Every time i'm trying to build the project i get:

Caused by: java.lang.UnsupportedOperationException: no descriptor for type constructor of TypeVariable(R)?
    at org.jetbrains.kotlin.load.kotlin.TypeSignatureMappingKt.mapType(typeSignatureMapping.kt:89)
    at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.mapType(KotlinTypeMapper.java:502)
    at org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.mapType(KotlinTypeMapper.java:456)
    at org.jetbrains.kotlin.codegen.CodegenUtilKt.asmType(codegenUtil.kt:274)
    at org.jetbrains.kotlin.codegen.CodegenUtilKt.asmType(codegenUtil.kt:277)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.expressionType(ExpressionCodegen.java:459)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitSafeQualifiedExpression(ExpressionCodegen.java:3159)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitSafeQualifiedExpression(ExpressionCodegen.java:108)
    at org.jetbrains.kotlin.psi.KtSafeQualifiedExpression.accept(KtSafeQualifiedExpression.kt:23)
    at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:31)
    at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:22)
    at org.jetbrains.kotlin.psi.KtVisitor.visitExpression(KtVisitor.java:182)
    at org.jetbrains.kotlin.psi.KtVisitor.visitQualifiedExpression(KtVisitor.java:286)
    at org.jetbrains.kotlin.psi.KtVisitor.visitSafeQualifiedExpression(KtVisitor.java:306)
    at org.jetbrains.kotlin.psi.KtSafeQualifiedExpression.accept(KtSafeQualifiedExpression.kt:23)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:296)
    ... 103 more

upd:
false alarm. i was using the new type inference model
-XXLanguage:+NewInference
i removed it and all works now

@nhajratw
Copy link

nhajratw commented Nov 6, 2018

@deviant-studio fyi -- my solution above is still working with kotlin 1.3

@soprater
Copy link

@deviant-studio fyi -- my solution above is still working with kotlin 1.3

@nhajratw I tried the following:

plugins {
// ...
    kotlin("kapt") version "1.3.61"
}
dependencies {
// ...
    implementation("com.querydsl:querydsl-jpa:4.2.2")
    kapt("com.querydsl:querydsl-apt:4.2.2:jpa")
}

Unfortunately I get a ClassNotFoundException where it says Did not find a query class ...QClass. It seems that the classes are not generated.
Do you know a fix to this issue?

@nhajratw
Copy link

@soprater sorry, i'm no longer working on the project where i had implemented this. Last i knew, it was working still. If i recall, you have to run the kaptGenerateStubsKotlin task in order to get the XXXQClass files.

@weitjong
Copy link

It is still working. Kotlin 1.3.61, Spring 2.2.5.RELEASE, querydsl 4.2.2. I have these:

    implementation("com.querydsl:querydsl-apt")
    implementation("com.querydsl:querydsl-jpa")

@F43nd1r
Copy link
Member

F43nd1r commented May 8, 2020

I've built a module to support emitting kotlin Qclasses. If there's interest I could upstream that.

@jwgmeligmeyling
Copy link
Member

I'd be interested in merging it upstream @F43nd1r .

Just to be clear: the intent of this feature is to generate QClasses in Kotlin right?

Because the current APT code generation works with Kotlin just fine, through Kapt. There is even a working example for it 😄 So QueryDSL "supports" Kotlin just fine.

@F43nd1r
Copy link
Member

F43nd1r commented Jan 24, 2021

Hi @jwgmeligmeyling. It has been a while since I worked on that fork. Yes, the intent is to generate kotlin QClasses.
I think I needed them because I had another tool which didn't work well with mixed sources.

Let me know how I can help.

@jwgmeligmeyling
Copy link
Member

Ah I'll see your changes are already available in https://github.com/querydsl/querydsl/compare/master...F43nd1r:kotlin?expand=1 .

I'll update them as soon as I get to it. I am first working on getting the 5.x release out.

@hameno
Copy link

hameno commented Nov 1, 2021

I'm trying to figure out how to use the new Kotlin support but cannot seem to find any documentation for this. Are there any examples/docs that I'm not seeing?
https://querydsl.com/static/querydsl/5.0.0/reference/html_single/ has no mention of Kotlin

Edit: Nevermind, actually found an example now at https://github.com/F43nd1r/querydsl/blob/master/querydsl-examples/querydsl-example-kotlin-codegen/pom.xml

@spyro2000
Copy link

Is there any documentation about how to get this to run with gradle?

@nhajratw
Copy link

@spyro2000 there are a few comments in the thread above (including mine) that have examples for gradle. That was a few years ago, so not sure if they still work, but probably a good starting point. Good luck!

@spyro2000
Copy link

Phew, ok. Thank you anyway :)

@amruthakm98
Copy link

Adding the following bits to build.gradle worked for me:

apply plugin: 'idea'
apply plugin: 'kotlin'
apply plugin: 'kotlin-jpa'
apply plugin: 'kotlin-kapt'

dependencies {
    //...
    implementation "com.querydsl:querydsl-jpa:5.0.0"
    kapt "com.querydsl:querydsl-apt:5.0.0:jpa"
}

idea {
    module {
        def kaptMain = file('build/generated/source/kapt/main')
        sourceDirs += kaptMain
        generatedSourceDirs += kaptMain
    }
}

kapt {
	generateStubs = true
}

@spyro2000
Copy link

@amruthakm98,

thank you! I wonder if this could be added to the documentation.

(for the authors of QueryDSL):

Please note that kapt is in 'maintenance mode' (maybe because it's awfully slow, like apt). Instead they want everone to use the more modern Kotlin Symbol Processing API https://kotlinlang.org/docs/ksp-overview.html

@jwgmeligmeyling
Copy link
Member

Well, Querydsl is an APT plugin and Kotlin supports APT plugins through KAPT. Using KAPT for APT plugins is pretty well documented within Kotlins own resources. Its a matter of who documents what 🤷🏻

But I believe there has been a Kotlin specific codegen module contributed to the project. It might be what you're looking for.

@F43nd1r
Copy link
Member

F43nd1r commented Jan 6, 2023

But I believe there has been a Kotlin specific codegen module contributed to the project. It might be what you're looking for.

Yeah I contributed that. But:
The module generates kotlin code instead of java. It changes nothing about how to call the code generator. Implementing an optional ksp layer next to apt would be a massive undertaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.