Hi. I’m trying to create a Post belongsTo User rel...
# spring
d
Hi. I’m trying to create a Post belongsTo User relationship query (the inverse of oneToMany). My user class is :
Copy code
data class UserDto(
        val id: Long?,

       ...

        var posts: List<PostDto>? = emptyList()
)
and my post:
Copy code
data class PostDto(
    val id: Long?,

    var userId: Long,

    ...
    var user: UserDto? = null
)
my function was going to be this:
Copy code
suspend fun findByIdWithUser(id: Long): PostDto? = coroutineScope {
        val post = async{findById(id)}
        val user = async{userRepository.findById(id)}
        return@coroutineScope post.await()?.copy(user = user.await())
    }
But it won’t compile because I get this error:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
Ideas anyone ?
j
Your post repository depends on your user repository which depends on ....?
👍 1
j
I would probably move findByIdWithUser(id to a service calling the 3 different repositories. And remove the circular dependency.
👍 1
d
I see what you mean. In my repos I’d do simple db queries and in the services I could do the relationships mappings.