lc-spring-data-r2dbc
lc-spring-data-r2dbc copied to clipboard
Joined property is not populated when using SelectQuery.join
Hi, I have those two (simplified) entities with a one-to-one relationship:
@Table("project")
data class ProjectEntity(
@Id
val id: ProjectId,
var name: String,
@ForeignKey(onForeignDeleted = ForeignKey.OnForeignDeleted.SET_TO_NULL, cascadeDelete = true)
var address: AddressEntity
)
@Table("address")
data class AddressEntity(
@Id
val id: AddressId,
var street: String = "",
var city: String = ""
)
And a repository:
@Repository
interface ProjectRepository: LcR2dbcRepository<ProjectEntity, ProjectId> {
override fun findById(id: ProjectId): Mono<ProjectEntity> {
return SelectQuery<ProjectEntity> = SelectQuery.from(ProjectEntity::class.java, "project")
.join("project", "address", "address")
.where(Criteria.property("project", "id").`is`(id))
.execute(lcClient)
.next()
}
}
When I try to use this findById function, I find myself with an empty AddressEntity in the address field. The only well-assigned field is the id (the others take their default values).
I did a little quick digging, and found out that it might be caused by the cache within the LcEntityReader.
When the project is first loaded during the execution of the query, it creates a first empty instance of the address and adds it to the cache. Then, when the fillLinkedEntities function is called for the joins, it finds the cached address and doesn't bother to read the query result to populate it.
Did I miss something in my setup or is it a bug?
PS: I had to add default values to the fields of AddressEntity because of the empty instance created during the query execution (I would get exceptions because of the null values). Is there any way to avoid that?
PPS: I saw in your wiki that default functions aren't supported in kotlin for the Spring Repositories. Actually, as I did, it is possible to add custom functions to a repository by using the compiler arg -Xjvm-default (and with the annotation @JvmDefault for older kotlin versions)