From 7105c9a375f051eccc39fd4fe0ec8b73f57a0a7e Mon Sep 17 00:00:00 2001 From: bagger3025 Date: Wed, 16 Oct 2024 07:56:09 +0900 Subject: [PATCH] fix: silently ignores exception when parameter setting to count query (#781) * fix: silently ignores exception when parameter setting to count query * fix: use map instead of try-catch * fix: test error fix and change map to set * fix: revert divided methods into one method * fix: debug logging when parameter binding is skipped * fix: minor grammar fix * fix: apply exception ignore to all in support * refactor: logging with string template * refactor: debug logic into else statement --- support/eclipselink-javax/build.gradle.kts | 1 + .../javax/JpqlEntityManagerUtils.kt | 17 +++++- .../javax/JpqlEntityManagerUtilsTest.kt | 33 ++++++++++++ support/eclipselink/build.gradle.kts | 1 + .../eclipselink/JpqlEntityManagerUtils.kt | 17 +++++- .../eclipselink/JpqlEntityManagerUtilsTest.kt | 33 ++++++++++++ support/hibernate-javax/build.gradle.kts | 1 + .../hibernate/JpqlEntityManagerUtils.kt | 17 +++++- .../hibernate/JpqlEntityManagerUtilsTest.kt | 33 ++++++++++++ .../hibernate-reactive-javax/build.gradle.kts | 1 + .../reactive/JpqlMutinySessionUtils.kt | 11 +++- .../JpqlMutinyStatelessSessionUtils.kt | 11 +++- .../reactive/JpqlStageSessionUtils.kt | 11 +++- .../JpqlStageStatelessSessionUtils.kt | 11 +++- support/hibernate-reactive/build.gradle.kts | 1 + .../reactive/JpqlMutinySessionUtils.kt | 19 ++++++- .../JpqlMutinyStatelessSessionUtils.kt | 19 ++++++- .../reactive/JpqlStageSessionUtils.kt | 19 ++++++- .../JpqlStageStatelessSessionUtils.kt | 19 ++++++- support/hibernate/build.gradle.kts | 1 + .../hibernate/JpqlEntityManagerUtils.kt | 17 +++++- .../hibernate/JpqlEntityManagerUtilsTest.kt | 33 ++++++++++++ support/spring-batch-javax/build.gradle.kts | 1 + .../batch/javax/JpqlEntityManagerUtils.kt | 17 +++++- .../batch/javax/JpqlEntityManagerUtilsTest.kt | 15 ++++++ support/spring-batch/build.gradle.kts | 1 + .../spring/batch/JpqlEntityManagerUtils.kt | 17 +++++- .../batch/JpqlEntityManagerUtilsTest.kt | 15 ++++++ .../data/jpa/javax/JpqlEntityManagerUtils.kt | 17 +++++- .../jpa/javax/JpqlEntityManagerUtilsTest.kt | 53 +++++++++++++++++++ .../spring/data/jpa/JpqlEntityManagerUtils.kt | 17 +++++- .../data/jpa/JpqlEntityManagerUtilsTest.kt | 53 +++++++++++++++++++ 32 files changed, 512 insertions(+), 20 deletions(-) diff --git a/support/eclipselink-javax/build.gradle.kts b/support/eclipselink-javax/build.gradle.kts index 8000c1859..08a5a6f41 100644 --- a/support/eclipselink-javax/build.gradle.kts +++ b/support/eclipselink-javax/build.gradle.kts @@ -1,6 +1,7 @@ dependencies { compileOnly(libs.eclipselink2) compileOnly(libs.javax.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/eclipselink-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtils.kt b/support/eclipselink-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtils.kt index b01e863d5..89c4cb4ae 100644 --- a/support/eclipselink-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtils.kt +++ b/support/eclipselink-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtils.kt @@ -2,6 +2,7 @@ package com.linecorp.kotlinjdsl.support.eclipselink.javax import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext +import org.slf4j.LoggerFactory import javax.persistence.EntityManager import javax.persistence.Query import javax.persistence.TypedQuery @@ -74,8 +75,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: Map) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/eclipselink-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtilsTest.kt b/support/eclipselink-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtilsTest.kt index 0bc3a241f..f8f0b14eb 100644 --- a/support/eclipselink-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtilsTest.kt +++ b/support/eclipselink-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/javax/JpqlEntityManagerUtilsTest.kt @@ -16,6 +16,7 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import javax.persistence.EntityManager +import javax.persistence.Parameter import javax.persistence.TypedQuery @ExtendWith(MockKExtension::class) @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + private val renderedQuery1 = "query" private val renderedParam1 = "queryParam1" to "queryParamValue1" private val renderedParam2 = "queryParam2" to "queryParamValue2" @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -71,6 +83,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -83,7 +98,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -95,6 +113,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -107,7 +128,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -118,6 +142,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -130,7 +157,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -142,6 +172,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } diff --git a/support/eclipselink/build.gradle.kts b/support/eclipselink/build.gradle.kts index f1200b5fd..e1d9116e3 100644 --- a/support/eclipselink/build.gradle.kts +++ b/support/eclipselink/build.gradle.kts @@ -1,6 +1,7 @@ dependencies { compileOnly(libs.eclipselink3) compileOnly(libs.jakarta.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/eclipselink/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtils.kt b/support/eclipselink/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtils.kt index ab93bdbbc..4c01b1604 100644 --- a/support/eclipselink/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtils.kt +++ b/support/eclipselink/src/main/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtils.kt @@ -5,6 +5,7 @@ import com.linecorp.kotlinjdsl.render.RenderContext import jakarta.persistence.EntityManager import jakarta.persistence.Query import jakarta.persistence.TypedQuery +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlEntityManagerUtils { @@ -74,8 +75,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: Map) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/eclipselink/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtilsTest.kt b/support/eclipselink/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtilsTest.kt index 8f07778fc..ec82ea2d9 100644 --- a/support/eclipselink/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtilsTest.kt +++ b/support/eclipselink/src/test/kotlin/com/linecorp/kotlinjdsl/support/eclipselink/JpqlEntityManagerUtilsTest.kt @@ -12,6 +12,7 @@ import io.mockk.junit5.MockKExtension import io.mockk.mockkObject import io.mockk.verifySequence import jakarta.persistence.EntityManager +import jakarta.persistence.Parameter import jakarta.persistence.TypedQuery import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.BeforeEach @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + private val renderedQuery1 = "query" private val renderedParam1 = "queryParam1" to "queryParamValue1" private val renderedParam2 = "queryParam2" to "queryParamValue2" @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -71,6 +83,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -83,7 +98,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -95,6 +113,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -107,7 +128,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -118,6 +142,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -130,7 +157,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -142,6 +172,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } diff --git a/support/hibernate-javax/build.gradle.kts b/support/hibernate-javax/build.gradle.kts index c4f394655..5247048d9 100644 --- a/support/hibernate-javax/build.gradle.kts +++ b/support/hibernate-javax/build.gradle.kts @@ -2,6 +2,7 @@ dependencies { @Suppress("VulnerableLibrariesLocal", "RedundantSuppression") compileOnly(libs.hibernate5.core) compileOnly(libs.javax.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/hibernate-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt b/support/hibernate-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt index 651b18f5a..a91b2b215 100644 --- a/support/hibernate-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt +++ b/support/hibernate-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt @@ -4,6 +4,7 @@ package com.linecorp.kotlinjdsl.support.hibernate import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext +import org.slf4j.LoggerFactory import javax.persistence.EntityManager import javax.persistence.Query import javax.persistence.TypedQuery @@ -76,8 +77,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: Map) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/hibernate-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt b/support/hibernate-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt index b67494ffe..5ba14e054 100644 --- a/support/hibernate-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt +++ b/support/hibernate-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt @@ -16,6 +16,7 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import javax.persistence.EntityManager +import javax.persistence.Parameter import javax.persistence.TypedQuery @ExtendWith(MockKExtension::class) @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + private val renderedQuery1 = "query" private val renderedParam1 = "queryParam1" to "queryParamValue1" private val renderedParam2 = "queryParam2" to "queryParamValue2" @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -71,6 +83,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -83,7 +98,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -95,6 +113,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -107,7 +128,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -118,6 +142,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -130,7 +157,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -142,6 +172,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } diff --git a/support/hibernate-reactive-javax/build.gradle.kts b/support/hibernate-reactive-javax/build.gradle.kts index 609758488..438bfdb47 100644 --- a/support/hibernate-reactive-javax/build.gradle.kts +++ b/support/hibernate-reactive-javax/build.gradle.kts @@ -1,6 +1,7 @@ dependencies { compileOnly(libs.hibernate.reactive1.core) compileOnly(libs.javax.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt index 6bc81ce75..d126571fa 100644 --- a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt +++ b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.mutiny.Mutiny +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlMutinySessionUtils { @@ -73,7 +74,15 @@ internal object JpqlMutinySessionUtils { private fun setParams(query: Mutiny.Query, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlMutinySessionUtils::class.java) diff --git a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt index 0e63c7070..ce4a3ef40 100644 --- a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt +++ b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.mutiny.Mutiny +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlMutinyStatelessSessionUtils { @@ -73,7 +74,15 @@ internal object JpqlMutinyStatelessSessionUtils { private fun setParams(query: Mutiny.Query, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlMutinyStatelessSessionUtils::class.java) diff --git a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt index e6d3b2db2..cffbe32bf 100644 --- a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt +++ b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.stage.Stage +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlStageSessionUtils { @@ -73,7 +74,15 @@ internal object JpqlStageSessionUtils { private fun setParams(query: Stage.Query, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlStageSessionUtils::class.java) diff --git a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt index cb8f5e5dc..6d2285a73 100644 --- a/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt +++ b/support/hibernate-reactive-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.stage.Stage +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlStageStatelessSessionUtils { @@ -73,7 +74,15 @@ internal object JpqlStageStatelessSessionUtils { private fun setParams(query: Stage.Query, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlStageStatelessSessionUtils::class.java) diff --git a/support/hibernate-reactive/build.gradle.kts b/support/hibernate-reactive/build.gradle.kts index f4a48ce1b..dce0bceca 100644 --- a/support/hibernate-reactive/build.gradle.kts +++ b/support/hibernate-reactive/build.gradle.kts @@ -1,6 +1,7 @@ dependencies { compileOnly(libs.hibernate.reactive2.core) compileOnly(libs.jakarta.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt index 340ed988f..9e048a14e 100644 --- a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt +++ b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinySessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.mutiny.Mutiny +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlMutinySessionUtils { @@ -73,13 +74,27 @@ internal object JpqlMutinySessionUtils { private fun setParams(query: Mutiny.SelectionQuery, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } private fun setParams(query: Mutiny.MutationQuery, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlMutinySessionUtils::class.java) diff --git a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt index 209055e65..ac81cdf98 100644 --- a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt +++ b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlMutinyStatelessSessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.mutiny.Mutiny +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlMutinyStatelessSessionUtils { @@ -73,13 +74,27 @@ internal object JpqlMutinyStatelessSessionUtils { private fun setParams(query: Mutiny.SelectionQuery, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } private fun setParams(query: Mutiny.Query, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlMutinyStatelessSessionUtils::class.java) diff --git a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt index c8c2e32b4..37dc59865 100644 --- a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt +++ b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageSessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.stage.Stage +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlStageSessionUtils { @@ -73,13 +74,27 @@ internal object JpqlStageSessionUtils { private fun setParams(query: Stage.SelectionQuery, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } private fun setParams(query: Stage.MutationQuery, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlStageSessionUtils::class.java) diff --git a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt index 6810a1959..5502ac605 100644 --- a/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt +++ b/support/hibernate-reactive/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/reactive/JpqlStageStatelessSessionUtils.kt @@ -3,6 +3,7 @@ package com.linecorp.kotlinjdsl.support.hibernate.reactive import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import org.hibernate.reactive.stage.Stage +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlStageStatelessSessionUtils { @@ -73,13 +74,27 @@ internal object JpqlStageStatelessSessionUtils { private fun setParams(query: Stage.SelectionQuery, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } private fun setParams(query: Stage.Query, params: Map) { params.forEach { (name, value) -> - query.setParameter(name, value) + try { + query.setParameter(name, value) + } catch (e: RuntimeException) { + if (log.isDebugEnabled) { + log.debug("Silently ignoring", e) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlStageStatelessSessionUtils::class.java) diff --git a/support/hibernate/build.gradle.kts b/support/hibernate/build.gradle.kts index 317a45270..c8b31e378 100644 --- a/support/hibernate/build.gradle.kts +++ b/support/hibernate/build.gradle.kts @@ -1,6 +1,7 @@ dependencies { compileOnly(libs.hibernate6.core) compileOnly(libs.jakarta.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/hibernate/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt b/support/hibernate/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt index 93acccd8d..f92a6a326 100644 --- a/support/hibernate/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt +++ b/support/hibernate/src/main/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtils.kt @@ -7,6 +7,7 @@ import com.linecorp.kotlinjdsl.render.RenderContext import jakarta.persistence.EntityManager import jakarta.persistence.Query import jakarta.persistence.TypedQuery +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlEntityManagerUtils { @@ -76,8 +77,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: Map) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/hibernate/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt b/support/hibernate/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt index 34635291b..a77fff747 100644 --- a/support/hibernate/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt +++ b/support/hibernate/src/test/kotlin/com/linecorp/kotlinjdsl/support/hibernate/JpqlEntityManagerUtilsTest.kt @@ -12,6 +12,7 @@ import io.mockk.junit5.MockKExtension import io.mockk.mockkObject import io.mockk.verifySequence import jakarta.persistence.EntityManager +import jakarta.persistence.Parameter import jakarta.persistence.TypedQuery import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.BeforeEach @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + private val renderedQuery1 = "query" private val renderedParam1 = "queryParam1" to "queryParamValue1" private val renderedParam2 = "queryParam2" to "queryParamValue2" @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -71,6 +83,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -83,7 +98,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -95,6 +113,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -107,7 +128,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -118,6 +142,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -130,7 +157,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -142,6 +172,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } diff --git a/support/spring-batch-javax/build.gradle.kts b/support/spring-batch-javax/build.gradle.kts index 2aa244fc9..b70314812 100644 --- a/support/spring-batch-javax/build.gradle.kts +++ b/support/spring-batch-javax/build.gradle.kts @@ -4,6 +4,7 @@ dependencies { @Suppress("VulnerableLibrariesLocal", "RedundantSuppression") compileOnly(libs.spring.batch4.infrastructure) compileOnly(libs.javax.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/spring-batch-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtils.kt b/support/spring-batch-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtils.kt index f4a2d283f..7a3a17865 100644 --- a/support/spring-batch-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtils.kt +++ b/support/spring-batch-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtils.kt @@ -4,6 +4,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext import com.linecorp.kotlinjdsl.render.jpql.JpqlRendered import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderedParams +import org.slf4j.LoggerFactory import javax.persistence.EntityManager import javax.persistence.Query import javax.persistence.TypedQuery @@ -33,8 +34,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: JpqlRenderedParams) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/spring-batch-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtilsTest.kt b/support/spring-batch-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtilsTest.kt index 0391a5277..590abeb26 100644 --- a/support/spring-batch-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-batch-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/javax/JpqlEntityManagerUtilsTest.kt @@ -16,6 +16,7 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import javax.persistence.EntityManager +import javax.persistence.Parameter import javax.persistence.TypedQuery @ExtendWith(MockKExtension::class) @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + private val renderedQuery1 = "query" private val renderedParam1 = "queryParam1" to "queryParamValue1" private val renderedParam2 = "queryParam2" to "queryParamValue2" @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -72,6 +84,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(selectQuery1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } diff --git a/support/spring-batch/build.gradle.kts b/support/spring-batch/build.gradle.kts index 3e00e6afb..e42f97e65 100644 --- a/support/spring-batch/build.gradle.kts +++ b/support/spring-batch/build.gradle.kts @@ -3,6 +3,7 @@ dependencies { compileOnly(libs.spring.boot3.starter) compileOnly(libs.spring.batch5.infrastructure) compileOnly(libs.jakarta.persistence.api) + compileOnly(libs.slf4j) compileOnly(projects.jpqlDsl) compileOnly(projects.jpqlQueryModel) compileOnly(projects.jpqlRender) diff --git a/support/spring-batch/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtils.kt b/support/spring-batch/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtils.kt index 374f83c7c..650dba396 100644 --- a/support/spring-batch/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtils.kt +++ b/support/spring-batch/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtils.kt @@ -7,6 +7,7 @@ import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderedParams import jakarta.persistence.EntityManager import jakarta.persistence.Query import jakarta.persistence.TypedQuery +import org.slf4j.LoggerFactory import kotlin.reflect.KClass internal object JpqlEntityManagerUtils { @@ -33,8 +34,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: JpqlRenderedParams) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/spring-batch/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtilsTest.kt b/support/spring-batch/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtilsTest.kt index 4656703ca..6a9e30801 100644 --- a/support/spring-batch/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-batch/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/batch/JpqlEntityManagerUtilsTest.kt @@ -12,6 +12,7 @@ import io.mockk.junit5.MockKExtension import io.mockk.mockkObject import io.mockk.verifySequence import jakarta.persistence.EntityManager +import jakarta.persistence.Parameter import jakarta.persistence.TypedQuery import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.BeforeEach @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + private val renderedQuery1 = "query" private val renderedParam1 = "queryParam1" to "queryParamValue1" private val renderedParam2 = "queryParam2" to "queryParamValue2" @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -72,6 +84,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(selectQuery1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } diff --git a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt index dd00be131..e0b5cbf83 100644 --- a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt +++ b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt @@ -4,6 +4,7 @@ package com.linecorp.kotlinjdsl.support.spring.data.jpa.javax import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery import com.linecorp.kotlinjdsl.render.RenderContext +import org.slf4j.LoggerFactory import org.springframework.data.domain.Sort import org.springframework.data.jpa.repository.query.QueryEnhancerFactoryAdaptor import javax.persistence.EntityManager @@ -97,8 +98,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: Map) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt index 8970b0efe..fa0d2a70e 100644 --- a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt @@ -19,6 +19,7 @@ import org.springframework.data.domain.Sort import org.springframework.data.jpa.repository.query.QueryEnhancer import org.springframework.data.jpa.repository.query.QueryEnhancerFactoryAdaptor import javax.persistence.EntityManager +import javax.persistence.Parameter import javax.persistence.TypedQuery @ExtendWith(MockKExtension::class) @@ -41,9 +42,21 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + @MockK private lateinit var longTypedQuery1: TypedQuery + @MockK + private lateinit var longTypedQueryParam1: Parameter + + @MockK + private lateinit var longTypedQueryParam2: Parameter + private val sort1 = Sort.by("property1") private val renderedQuery1 = "query" @@ -67,6 +80,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } excludeRecords { longTypedQuery1.equals(any()) } + excludeRecords { longTypedQueryParam1.hashCode() } + excludeRecords { longTypedQueryParam2.hashCode() } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } } @Test @@ -76,7 +93,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -87,6 +107,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -99,7 +122,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -111,6 +137,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -123,7 +152,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -134,6 +166,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -146,7 +181,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -158,6 +196,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -175,8 +216,14 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 andThen longTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first + every { longTypedQuery1.parameters } returns setOf(longTypedQueryParam1, longTypedQueryParam2) every { longTypedQuery1.setParameter(any(), any()) } returns longTypedQuery1 + every { longTypedQueryParam1.name } returns renderedParam1.first + every { longTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -193,11 +240,17 @@ class JpqlEntityManagerUtilsTest : WithAssertions { queryEnhancer.applySorting(sort1) entityManager.createQuery(sortedQuery1, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) queryEnhancer.createCountQueryFor() entityManager.createQuery(countQuery1, Long::class.javaObjectType) + longTypedQuery1.parameters + longTypedQueryParam1.name + longTypedQueryParam2.name longTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) longTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } diff --git a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt index 5265e69bc..58d53d322 100644 --- a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt +++ b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt @@ -7,6 +7,7 @@ import com.linecorp.kotlinjdsl.render.RenderContext import jakarta.persistence.EntityManager import jakarta.persistence.Query import jakarta.persistence.TypedQuery +import org.slf4j.LoggerFactory import org.springframework.data.domain.Sort import org.springframework.data.jpa.repository.query.QueryEnhancerFactoryAdaptor import kotlin.reflect.KClass @@ -97,8 +98,22 @@ internal object JpqlEntityManagerUtils { } private fun setParams(query: Query, params: Map) { + val parameterNameSet = query.parameters.map { it.name }.toHashSet() + params.forEach { (name, value) -> - query.setParameter(name, value) + if (parameterNameSet.contains(name)) { + query.setParameter(name, value) + } else { + if (log.isDebugEnabled) { + log.debug( + "No parameter named '$name' in query " + + "with named parameters [${parameterNameSet.joinToString()}], " + + "parameter binding skipped", + ) + } + } } } } + +private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java) diff --git a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt index aecd654a2..373200dd9 100644 --- a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt @@ -12,6 +12,7 @@ import io.mockk.junit5.MockKExtension import io.mockk.mockkObject import io.mockk.verifySequence import jakarta.persistence.EntityManager +import jakarta.persistence.Parameter import jakarta.persistence.TypedQuery import org.assertj.core.api.WithAssertions import org.junit.jupiter.api.BeforeEach @@ -41,9 +42,21 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var stringTypedQuery1: TypedQuery + @MockK + private lateinit var stringTypedQueryParam1: Parameter + + @MockK + private lateinit var stringTypedQueryParam2: Parameter + @MockK private lateinit var longTypedQuery1: TypedQuery + @MockK + private lateinit var longTypedQueryParam1: Parameter + + @MockK + private lateinit var longTypedQueryParam2: Parameter + private val sort1 = Sort.by("property1") private val renderedQuery1 = "query" @@ -66,7 +79,11 @@ class JpqlEntityManagerUtilsTest : WithAssertions { excludeRecords { JpqlRendererHolder.get() } excludeRecords { stringTypedQuery1.equals(any()) } + excludeRecords { stringTypedQueryParam1.hashCode() } + excludeRecords { stringTypedQueryParam2.hashCode() } excludeRecords { longTypedQuery1.equals(any()) } + excludeRecords { longTypedQueryParam1.hashCode() } + excludeRecords { longTypedQueryParam2.hashCode() } } @Test @@ -76,7 +93,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context) @@ -87,6 +107,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -99,7 +122,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -111,6 +137,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -123,7 +152,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context) @@ -134,6 +166,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -146,7 +181,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any(), any()) } returns rendered1 every { entityManager.createQuery(any()) } returns stringTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -158,6 +196,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { verifySequence { renderer.render(query1, mapOf(queryParam1, queryParam2), context) entityManager.createQuery(rendered1.query) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) } @@ -175,8 +216,14 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 andThen longTypedQuery1 + every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2) every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 + every { stringTypedQueryParam1.name } returns renderedParam1.first + every { stringTypedQueryParam2.name } returns renderedParam2.first + every { longTypedQuery1.parameters } returns setOf(longTypedQueryParam1, longTypedQueryParam2) every { longTypedQuery1.setParameter(any(), any()) } returns longTypedQuery1 + every { longTypedQueryParam1.name } returns renderedParam1.first + every { longTypedQueryParam2.name } returns renderedParam2.first // when val actual = JpqlEntityManagerUtils @@ -193,11 +240,17 @@ class JpqlEntityManagerUtilsTest : WithAssertions { queryEnhancer.applySorting(sort1) entityManager.createQuery(sortedQuery1, String::class.java) + stringTypedQuery1.parameters + stringTypedQueryParam1.name + stringTypedQueryParam2.name stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) queryEnhancer.createCountQueryFor() entityManager.createQuery(countQuery1, Long::class.javaObjectType) + longTypedQuery1.parameters + longTypedQueryParam1.name + longTypedQueryParam2.name longTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) longTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second) }