I would like to generate a query with my R2dbcRepository
. In Spring Data JPA
the documentation says to use #{#entityName}
to get an Entity
if a repository is generic, however I can't find the equivalent in R2DBC
.
I'm using a H2 in memory database: r2dbc:h2:mem:///~/db/testdb
R2dbcRepository
public interface UUIDRepository<Model, IdType> extends R2dbcRepository<Model, IdType> {
Mono<Model> findByIdAndUserUUID(Long id, UUID uuid);
@Query("SELECT * FROM #{#entityName} WHERE model.id = :id AND (model.user_uuid = :uuid OR model.user_uuid IS NULL)")
Mono<Model> findByIdAndUserUUIDOrUserUUIDIsNull(@Param("id") Long id, @Param("uuid") UUID uuid);
}
Exception
Caused by: io.r2dbc.spi.R2dbcBadGrammarException: [42000] [42000] Syntax error in SQL statement "FROM [*]#{#entityName} model WHERE model.id = $1 AND (model.user_uuid = $2 OR model.user_uuid IS NULL)"; SQL statement:
I know the syntax for the rest of the query is correct as if I replace #{#entityName}
with a table name it works fine.
How can I insert the entity name in a query when using R2DBC
?
How can I insert the entity name in a query when using R2DBC?
Short answer, you don't.
R2DBC don't have the concept of entities in the same sense as JPA does. You write native queries when you write queries for R2DBC.
So, it's not JPQL and therefore the query language don't have any concept of an entity representation from your code.
However the repository still supports an auto mapping from your database resulting column information per row to a list of (data) objects. That's the reason why it works when you replace your entity name with the table name.