Is there any way to simplify this kotlin + spring boot JPA code? 
(especially the findAllBy)
Take note that I want to avoid doing joins with JPA. 

@Repository
class TrackingConfigurationsRepository(
    private val trackingConfigurationJpaRepository: TrackingConfigurationJpaRepository,
    private val genericTrackingMethodJpaRepository: GenericTrackingMethodJpaRepository,
    private val trackingConfigurationGenericTrackingMethodJpaRepository: TrackingConfigurationGenericTrackingMethodJpaRepository,
) : TrackingConfigurations {
    override fun findByIdOrNull(id: TrackingConfigurationId): TrackingConfiguration? = trackingConfigurationJpaRepository.findByIdOrNull(id.value)?.toDomain()

    override fun findAllBy(
        genericTrackingMethodId: Int?,
        deliveryOfferIds: List<Int>?,
        pagination: Pagination,
    ): Page<TrackingConfigurationWithGenericMethods> {
        val trackingConfigurations =
            when {
                genericTrackingMethodId != null && deliveryOfferIds != null -> {
                    trackingConfigurationJpaRepository.findAllByDefaultTrackingMethodIdAndDeliveryOfferIdInOrderByIdAsc(
                        genericTrackingMethodId,
                        deliveryOfferIds,
                        pagination.toPageable(),
                    )
                }

                deliveryOfferIds != null -> {
                    trackingConfigurationJpaRepository.findAllByDeliveryOfferIdInOrderByIdAsc(
                        deliveryOfferIds,
                        pagination.toPageable(),
                    )
                }

                genericTrackingMethodId != null -> {
                    trackingConfigurationJpaRepository.findAllByDefaultTrackingMethodIdOrderByIdAsc(
                        genericTrackingMethodId,
                        pagination.toPageable(),
                    )
                }

                else -> {
                    trackingConfigurationJpaRepository.findAllByOrderByIdAsc(pagination.toPageable())
                }
            }

        val genericTrackingMethodIds =
            trackingConfigurationGenericTrackingMethodJpaRepository.findAllByTrackingConfigurationIdIn(trackingConfigurations.map { it.id }.toSet())

        val genericTrackingMethodsById =
            genericTrackingMethodJpaRepository
                .findAllById(genericTrackingMethodIds.map { it.genericTrackingMethodId }.toSet())
                .associateBy { it.id }

        val genericTrackingMethodsByTrackingConfigurationId =
            buildMap<Int, Collection<GenericTrackingMethodJpa>> {
                genericTrackingMethodIds
                    .groupBy { it.trackingConfigurationId }
                    .mapValues { it.value.map { it.genericTrackingMethodId } }
                    .forEach { (trackingConfigurationId, genericTrackingMethodIds) ->
                        put(trackingConfigurationId, genericTrackingMethodsById.filterKeys { it: Int -> it in genericTrackingMethodIds }.values)
                    }
            }

        return trackingConfigurations.map { trackingConfigurationJpa ->
            trackingConfigurationJpa.toDomain(genericTrackingMethodsByTrackingConfigurationId.getValue(trackingConfigurationJpa.id))
        }
    }
}

fun TrackingConfigurationJpa.toDomain(genericTrackingMethods: Collection<GenericTrackingMethodJpa>): TrackingConfigurationWithGenericMethods =
    TrackingConfigurationWithGenericMethods(
        trackingConfiguration = this.toDomain(),
        genericTrackingMethods = genericTrackingMethods.map { it.toDomain() },
    )

fun TrackingConfigurationJpa.toDomain(): TrackingConfiguration =
    TrackingConfiguration(
        id = TrackingConfigurationId(id),
        deliveryOfferId = deliveryOfferId,
        defaultTrackingMethodId = defaultTrackingMethodId,
        urlGeneratorSlug = urlGeneratorSlug,
    )