#440 - Create R2dbcEntityTemplate in R2dbcRepositoryFactoryBean.

We now create R2dbcEntityTemplate as part of R2dbcRepositoryFactoryBean initialization if the factory bean was configured with DatabaseClient and DataAccessStrategy only. Creating the template in the factory bean allows collecting entity callbacks for repository usage.
This commit is contained in:
Mark Paluch
2020-09-03 11:07:27 +02:00
parent dc8eb79797
commit b4002e5c3f
4 changed files with 48 additions and 3 deletions

View File

@@ -366,6 +366,7 @@ public abstract class AbstractR2dbcRepositoryIntegrationTests extends R2dbcInteg
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
static class Lego {
@Id Integer id;
}

View File

@@ -37,12 +37,14 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.annotation.Id;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.r2dbc.testing.ExternalDatabase;
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -70,6 +72,23 @@ public class PostgresR2dbcRepositoryIntegrationTests extends AbstractR2dbcReposi
public ConnectionFactory connectionFactory() {
return PostgresTestSupport.createConnectionFactory(database);
}
@Bean
public BeforeConvertCallback<LegoSet> autogeneratedId(DatabaseClient client) {
return (entity, table) -> {
if (entity.getId() == null) {
return client.sql("SELECT nextval('person_seq');") //
.map(row -> row.get(0, Integer.class)) //
.first() //
.doOnNext(entity::setId) //
.thenReturn(entity);
}
return Mono.just(entity);
};
}
}
@Override
@@ -84,7 +103,7 @@ public class PostgresR2dbcRepositoryIntegrationTests extends AbstractR2dbcReposi
@Override
protected String getCreateTableStatement() {
return PostgresTestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION;
return PostgresTestSupport.CREATE_TABLE_LEGOSET + ";CREATE SEQUENCE IF NOT EXISTS person_seq;";
}
@Override