Skip to content

Commit 2cefe51

Browse files
authored
Merge branch 'master' into dependabot/maven/spring.version-6.2.1
2 parents a4f4a54 + 28b0d8b commit 2cefe51

File tree

5 files changed

+80
-7
lines changed

5 files changed

+80
-7
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package com.querydsl.example.ksp
22

33
import jakarta.persistence.Entity
4+
import jakarta.persistence.Enumerated
5+
import jakarta.persistence.EnumType
46
import jakarta.persistence.Id
7+
import jakarta.persistence.ManyToOne
58

69
@Entity
710
class Cat(
811
@Id
912
val id: Int,
1013
val name: String,
11-
val isTailUpright: Boolean
14+
val isTailUpright: Boolean,
15+
val fluffiness: String? = null,
16+
17+
@ManyToOne
18+
val owner: Person? = null,
19+
20+
@Enumerated(EnumType.STRING)
21+
val catType: CatType? = CatType.HOUSE,
1222
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.querydsl.example.ksp
2+
3+
enum class CatType {
4+
HOUSE,
5+
MAINE_COON
6+
}

querydsl-examples/querydsl-example-ksp-codegen/src/main/kotlin/com/querydsl/example/ksp/Person.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package com.querydsl.example.ksp
22

33
import jakarta.persistence.Entity
44
import jakarta.persistence.Id
5+
import jakarta.persistence.OneToMany
56

67
@Entity
78
class Person(
89
@Id
910
val id: Int,
1011
val name: String,
12+
13+
@OneToMany(mappedBy = "owner")
14+
val cats: List<Cat>? = null,
1115
)

querydsl-examples/querydsl-example-ksp-codegen/src/test/kotlin/Tests.kt

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.querydsl.example.ksp.Cat
2+
import com.querydsl.example.ksp.CatType
23
import com.querydsl.example.ksp.Person
34
import com.querydsl.example.ksp.QCat
45
import com.querydsl.example.ksp.QPerson
@@ -40,15 +41,48 @@ class Tests {
4041
}
4142
}
4243

44+
@Test
45+
fun `create cat with owner` () {
46+
val emf = initialize()
47+
48+
run {
49+
val em = emf.createEntityManager()
50+
em.transaction.begin()
51+
val catOwner = Person(425, "Percy Bysshe Catownerly")
52+
em.persist(catOwner)
53+
em.persist(Cat(103, "Samuel Taylor Cattingridge", false, "Not so fluffy", catOwner, CatType.MAINE_COON))
54+
em.transaction.commit()
55+
em.close()
56+
}
57+
58+
run {
59+
val em = emf.createEntityManager()
60+
val queryFactory = JPAQueryFactory(em)
61+
val q = QCat.cat
62+
val catWithOwner = queryFactory
63+
.selectFrom(q)
64+
.where(q.name.eq("Samuel Taylor Cattingridge"))
65+
.fetchOne()
66+
if (catWithOwner == null) {
67+
fail<Any>("No owned cat was returned")
68+
} else {
69+
assertThat(catWithOwner.id).isEqualTo(103)
70+
assertThat(catWithOwner.catType).isEqualTo(CatType.MAINE_COON)
71+
assertThat(catWithOwner.owner?.id).isEqualTo(425)
72+
}
73+
em.close()
74+
}
75+
}
76+
4377
@Test
4478
fun `filter entities`() {
4579
val emf = initialize()
4680

4781
run {
4882
val em = emf.createEntityManager()
4983
em.transaction.begin()
50-
em.persist(Cat(100, "Neville Furbottom", true))
51-
em.persist(Cat(101, "Edgar Allan Paw", true))
84+
em.persist(Cat(100, "Neville Furbottom", true, "Quite Fluffy"))
85+
em.persist(Cat(101, "Edgar Allan Paw", true, null))
5286
em.persist(Cat(102, "Admiral Meowington", false))
5387
em.transaction.commit()
5488
em.close()
@@ -66,6 +100,13 @@ class Tests {
66100
assertThat(animals.size).isEqualTo(2)
67101
assertThat(animals).contains("Neville Furbottom")
68102
assertThat(animals).contains("Edgar Allan Paw")
103+
val quiteFluffyAnimals = queryFactory
104+
.selectFrom(q)
105+
.where(q.fluffiness.eq("Quite Fluffy"))
106+
.fetch()
107+
.map { it.name }
108+
assertThat(quiteFluffyAnimals.size).isEqualTo(1)
109+
assertThat(quiteFluffyAnimals).contains("Neville Furbottom")
69110
em.close()
70111
}
71112
}

querydsl-tooling/querydsl-ksp-codegen/src/main/kotlin/com/querydsl/ksp/codegen/TypeExtractor.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TypeExtractor(
4545
}
4646

4747
private fun simpleType(type: KSType): QPropertyType.Simple? {
48-
val className = type.toClassName()
48+
val className = type.toClassNameSimple()
4949
val simpleType = SimpleType.Mapper.get(className)
5050
if (simpleType == null) {
5151
return null
@@ -61,7 +61,7 @@ class TypeExtractor(
6161
if (declaration is KSClassDeclaration) {
6262
types.addAll(declaration.getAllSuperTypes())
6363
}
64-
val classNames = types.map { it.toClassName() }
64+
val classNames = types.map { it.toClassNameSimple() }
6565
return if (classNames.any { it.isMap() }) {
6666
assertTypeArgCount(type, "map", 2)
6767
val keyType = extract(type.arguments[0].type!!.resolve())
@@ -86,7 +86,7 @@ class TypeExtractor(
8686
val referencedDeclaration = type.declaration
8787
if (referencedDeclaration is KSClassDeclaration) {
8888
return when (referencedDeclaration.classKind) {
89-
ClassKind.ENUM_CLASS -> QPropertyType.EnumReference(type.toClassName())
89+
ClassKind.ENUM_CLASS -> QPropertyType.EnumReference(type.toClassNameSimple())
9090
ClassKind.CLASS, ClassKind.INTERFACE -> {
9191
if (isEntity(referencedDeclaration)) {
9292
return QPropertyType.ObjectReference(
@@ -115,7 +115,7 @@ class TypeExtractor(
115115
Convert::class.asClassName()
116116
)
117117
if (property.annotations.any { userTypeAnnotations.contains(it.annotationType.resolve().toClassName()) }) {
118-
return QPropertyType.Unknown(type.toClassName(), type.toTypeName())
118+
return QPropertyType.Unknown(type.toClassNameSimple(), type.toTypeName())
119119
} else {
120120
return null
121121
}
@@ -156,3 +156,15 @@ private fun ClassName.isList(): Boolean {
156156
private fun ClassName.isArray(): Boolean {
157157
return this == Array::class.asClassName()
158158
}
159+
160+
/**
161+
* Ignores arguments and nullability, returning simple ClassName.
162+
*/
163+
private fun KSType.toClassNameSimple(): ClassName {
164+
return when (val decl = declaration) {
165+
is KSClassDeclaration -> decl.toClassName()
166+
is KSTypeAlias -> decl.toClassName()
167+
is KSTypeParameter -> error("Cannot convert KSTypeParameter to ClassName: '$this'")
168+
else -> error("Could not compute ClassName for '$this'")
169+
}
170+
}

0 commit comments

Comments
 (0)