Skip to content

Support interface projections with DatabaseClient.as(…) #260

@rj-hwang

Description

@rj-hwang

Use DatabaseClient to do interface projection failed!

Code:

interface UserView {
  val id: Int,
  val name: String
}

fun find(): Flux<UserView> {
  val sql = "select id, name from user"
  return databaseClient.execute(sql).`as`(UserView::class.java).fetch().all()
}

Error:

Suppressed: org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate ...UserView using constructor NO_CONSTRUCTOR with arguments 
  at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:67)
  at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:84)
  at org.springframework.data.relational.core.conversion.BasicRelationalConverter.createInstance(BasicRelationalConverter.java:146)
  at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.createInstance(MappingR2dbcConverter.java:235)
  at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.read(MappingR2dbcConverter.java:119)
  at org.springframework.data.r2dbc.convert.MappingR2dbcConverter.read(MappingR2dbcConverter.java:114)
  at org.springframework.data.r2dbc.convert.EntityRowMapper.apply(EntityRowMapper.java:46)
  at org.springframework.data.r2dbc.convert.EntityRowMapper.apply(EntityRowMapper.java:29)
  at io.r2dbc.h2.H2Result.lambda$map$0(H2Result.java:67)
  at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100)
  ... 122 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [...UserView]: Specified class is an interface
  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:132)
  at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64)
  ... 131 more

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions