Replies: 1 comment 1 reply
-
data class MoneySum(
val expr: Expression<Money>,
val distinct: Boolean = false,
) : Expression<Long>
class JpqlMoneySumSerializer : JpqlSerializer<MoneySum> {
override fun handledType(): KClass<MoneySum> = MoneySum::class
override fun serialize(part: MoneySum, writer: JpqlWriter, context: RenderContext) {
val delegate = context.getValue(JpqlRenderSerializer)
writer.write("SUM")
writer.writeParentheses {
if (part.distinct) {
writer.write("DISTINCT")
writer.write(" ")
}
delegate.serialize(part.expr, writer, context)
}
}
}
class MyJpql : Jpql() {
companion object Constructor : JpqlDsl.Constructor<MyJpql> {
override fun newInstance(): MyJpql = MyJpql()
}
fun <T : Any> sum(expr: KProperty1<T, Money?>): Expression<Long> = MoneySum(Paths.path(expr), false)
fun sum(expr: Expression<Money>, distinct: Boolean = false): Expression<Long> = MoneySum(expr, distinct)
}
@Configuration
class CustomJpqlRenderContextConfig {
@Bean
fun jpqlSerializer(): JpqlSerializer<*> = ValueClassAwareJpqlValueSerializer(JpqlValueSerializer())
@Bean
fun jpqlMoneySumSerializer(): JpqlSerializer<*> = JpqlMoneySumSerializer()
}
해결했습니다. ^^; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
WoongE
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
안녕하세요!
재미있는 라이브러리를 만들어주신 덕분에 흥미롭게 PoC 를 해보고 있습니다. :)
라이브러리 사용법 관련해서 질문이 있습니다.
제가 현재 작업하고 있는 프로젝트에서는 돈 관련 필드에 대해서는 전부 Money 클래스(Long 타입을 value class 로 래핑)를 만들어서 사용하고 있습니다. 그리고 이 Money 클래스를 Jpa Entity 클래스에서도 필드 타입으로 사용하고 있습니다.
JDSL 에서 Money 의 eq, gt, lt 등 비교구문은 문제없이 작동했으나, sum 이 불가능하여 방법을 찾아봤습니다만,
JpqlSum 에 MoneySum 을 추가할 방법이 현재로서는 사실상 없는 거 같더라고요.
혹시 이 부분에 대한 가이드가 있을까요?
Beta Was this translation helpful? Give feedback.
All reactions