lundi 27 février 2023

Using KProperty with List of objects

We're using Spring Data MongoDB with Kotlin. Kotlin comes with some nice typesafe Criteria Extensions (org.springframework.data.mongodb.core.query.TypedCriteriaExtensions).

given:

data class Foo(
  val id: String, 
  val oneBar: Bar, 
  val bars: List<Bar>)

data class Bar(val thingy: String)

You can use it to build Criteria in a typesafe manner, such as:

criterion = criterion + (Foo::id isEqualTo "id")

If you have a nested object Bar, you can use

criterion = criterion + (Foo::oneBar / Bar::thingy isEqualTo "thingy")

How do you use KProperty when you have an object Foo with a list of Bars and you want to write a Criteria based on a field of Bar

What I want to do is to filter on a property of Bar.

This doesn't work, but I want to do something like this:

criterion = criterion + (Foo::bars / <thing with list> / Bar::thingy isEqualTo "thingy")




Aucun commentaire:

Enregistrer un commentaire