Paths

Kotlin JDSL์€ JPQL์˜ path expression์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ Path ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Path๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด, path() ์™€ invoke()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// Book.isbn.value
path(Book::isbn).path(Isbn::value)
path(Book::isbn)(Isbn::value)

// b.isbn.value
entity(Book::class, "b").path(Book::isbn).path(Isbn::value)
entity(Book::class, "b")(Book::isbn)(Isbn::value)

Java entity

path() ์™€ invoke()๋Š” KProperty1 ๋˜๋Š” KFuction1๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. KFunction1์˜ ๊ฒฝ์šฐ, getter๋งŒ public์ธ Java๋กœ ์„ ์–ธํ•œ entity๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

@Entity
public class Book {
    @Id
    private Long id;

    private String title;

    public String getTitle() {
        return title;
    }
}
// compile error
path(Book::title)

// Book.title
path(Book::getTitle)

Kotlin JDSL์€ getter ์ด๋ฆ„์—์„œ ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„์„ ์ถ”๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ๊ทœ์น™์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

  • is๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • get์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ get์„ ์ œ๊ฑฐํ•˜๊ณ  ์ดํ›„ ์ฒซ ๊ธ€์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

  • ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ, ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

// Book.isAvailable
path(Book::isAvailable)

// Book.available
path(Book::getAvailable)

์œ„ ๊ทœ์น™ ๋Œ€์‹  ๋‚˜๋งŒ์˜ ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, JpqlPropertyIntrospector๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์ด๋ฅผ ์ด๋ฅผ RenderContext์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ Custom DSL์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. Spring์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด Spring supports๋„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

class MyIntrospector : JpqlPropertyIntrospector() {
    override fun introspect(property: KCallable<*>): JpqlPropertyDescription? {
        if (property is KFunction1<*, *>) {
            // ๋‚˜๋งŒ์˜ ๊ทœ์น™์œผ๋กœ ์ด๋ฆ„์„ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค
            val name = ...
            return MyProperty(name)
        }

        return null
    }

    private data class MyProperty(
        override val name: String,
    ) : JpqlPropertyDescription
}

val myModule = object : JpqlRenderModule {
    override fun setupModule(context: JpqlRenderModule.SetupContext) {
        context.prependIntrospector(MyIntrospector())
    }
}

val myContext = JpqlRenderContext().registerModule(myModule)

Expression

Path๋Š” select clause ๋‚˜ predicate ๋“ฑ์—์„œ Expression์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// SELECT Book.isbn FROM Book AS Book WHERE Book.isbn.value = :param1
jpql {
    select(
        path(Book::isbn),
    ).from(
        entity(Book::class),
    ).where(
        path(Book::isbn)(Ibsn::value).eq("01"),
    )
}

Treat

Path์˜ ํƒ€์ž…์„ ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด, treat()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

path(EmployeeDepartment::employee).treat(FullTimeEmployee::class)

Last updated