Skip to content

Autowire RelationalEntityInformation instance into CustomRepository #2021

Closed
@chinshaw

Description

@chinshaw

Apologies I don't know if this is a feature request or if I am completely missing something, however I am stuck trying to come up with a solution to getting the domain entity type during custom repository initialization (RepositoryFragment). It looks like the SimpleR2dbcRepsitory is initialized with RelationalEntityInformation<T,ID> but it seems that the beanfactories used between SimpleR2dbcRepository and the RepositoryFragmentFactory bean have a different contexts.

I went through pretty much all the spring docs and tried quite a few things including trying to use the newish feature for RepositoryMethodContext.getContext() during initialization but was hesitant to try it during function callback because of the note on the performance hit.

I could probably add a workaround to initialize my "tablemetadata" during the first upsert call but this feels dirty to me.

Appreciate any feedback, I may be missing something completely or my implementation maybe covered by some other functionality all together.

Here is a sample of what I am trying to accomplish and let me know if I am completely off base and there is a much cleaner way to do this.

interface SpringCoroutineRepositoryExample : CoroutineCrudRepository<SampleEntity,Xid>

// Spring Custom Repository 
interface UpsertRepository<T : IDomainEntity<Xid>> {
    fun upsert(values: List<T>): Flow<T>
}

// NOTE: Construction fails due to missing RelationalEntityInformation
class UpsertRepositoryImpl<T : IDomainEntity<Xid>>(
    entity: RelationalEntityInformation<T, Any>,
    r2dbcEntityTemplate: R2dbcEntityTemplate,
    auditorAware: ReactiveAuditorAware<String>
) : UpsertRepository<T> {
        
    init {
        // initialize table meta data using 
        val kclass = entity.javaType.kotlin
        // Using kclass initialize my custom table data
        initializeTableMetaData(kclass)
    }
    
    // Custom reflection to introspect columns
    private fun initializeTableMetaData(kClass: KClass<T>) {}

    // For brevity ... use my table meta data and the DatabaseClient to construct an upsert
    // 
    //  INSERT INTO ${tableMetaData.tableName} ($insertColumns)
    //     VALUES :tuples
    //   ON CONFLICT (${tableMetaData.uniqueKey})
    //      DO UPDATE SET
    //      $updateColumns
    //   RETURNING *;
    override fun upsert(values: List<T>): Flow<T> = emptyFlow()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions