Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joined property is not populated when using SelectQuery.join #14

Open
lheydel opened this issue Jul 26, 2022 · 0 comments
Open

Joined property is not populated when using SelectQuery.join #14

lheydel opened this issue Jul 26, 2022 · 0 comments

Comments

@lheydel
Copy link

lheydel commented Jul 26, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant