path() and invoke() can take KProperty1 or KFuction1 as an argument. KFunction1 is useful when you use Java entity with private property and public getter.
@Entity
public class Book {
@Id
private Long id;
private String title;
public String getTitle() {
return title;
}
}
If you want to use your own rule, implement JpqlPropertyIntrospector and provide it to RenderContext. See Custom DSL for more details. If you are using Spring, see Spring supports also.
class MyIntrospector : JpqlPropertyIntrospector() {
override fun introspect(property: KCallable<*>): JpqlPropertyDescription? {
if (property is KFunction1<*, *>) {
val name = // resolve a name with your own rule
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)
// 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"),
)
}