Spring Data JPA query method is throwing weird error message when trying to find all entities by a List property

Viewed 76

I want to write a simple query method that fetches all items by a list of traits given. Here is my test code that throws an InvalidPersistentPropertyPath exception.

  // BaseItemRepositoryTest.kt

  fun `should return items by trait`() {
        val expectedTrait = TraitTemplate.testTrait(code = "FOO")
        val expectedItem = ItemTemplate.testItem(traits = mutableListOf(expectedTrait))

        baseItemRepository.saveAll(
            listOf(
                expectedItem,
                ItemTemplate.testItem(),
                ItemTemplate.testItem()
            )
        )

        val actual = baseItemRepository.findAllByTraitsContaining(listOf(expectedTrait))
        assertThat(actual).containsOnly(expectedItem)
    }

I persist three items where one of those (called expected) is the one that has the matching trait value assigned.

I use the findAllByTraitsContaining query method to fetch items that only contain the given traits.

The exception thrown is the following:

No property 'traits' found on class com.ruckpack.equipmentservice.repository.document.BaseItem! Did you mean: traits?
org.springframework.data.mapping.context.InvalidPersistentPropertyPath: No property 'traits' found on class com.ruckpack.equipmentservice.repository.document.BaseItem! Did you mean: traits?
    at org.springframework.data.mapping.context.PersistentPropertyPathFactory.createPersistentPropertyPath(PersistentPropertyPathFactory.java:205)
    at org.springframework.data.mapping.context.PersistentPropertyPathFactory.lambda$getPersistentPropertyPath$1(PersistentPropertyPathFactory.java:172)
    at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330)
    at org.springframework.data.mapping.context.PersistentPropertyPathFactory.getPersistentPropertyPath(PersistentPropertyPathFactory.java:171)
    at org.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:86)
    at org.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:99)

The problem with that exception is that it suggests the trait variable even tho it beforehand tells me that this is the cause of the error. That is why I am confused.

Please see the relevant code snippets. I have a hard time figuring out what goes wrong. I tried different query methods already like findAllByTraitsIn etc. They all throw the same error.

// Items.kt

@Serializable
@Document(collection = "items")
sealed class BaseItem {
    abstract val id: String
    abstract var brand: String
    abstract var model: String
    abstract var imageLink: String
    abstract var traits: MutableList<Trait>
    abstract var implicitTraits: MutableList<Trait>
    abstract var details: MutableMap<String, String>
}

@Serializable
data class Item(
    override val id: String = UUID.randomUUID().toString(),
    override var brand: String,
    override var model: String,
    override var imageLink: String,
    override var traits: MutableList<Trait>,
    override var implicitTraits: MutableList<Trait>,
    override var details: MutableMap<String, String>,
) : BaseItem()
// BaseItemRepository.kt

@Repository
interface BaseItemRepository : MongoRepository<BaseItem, String> {
    fun findAllByTraitsContaining(traits: List<Trait>): List<BaseItem>
}
0 Answers
Related