Skip to content

Commit c23485d

Browse files
committed
#413 - Introduce constructor to R2dbcEntityTemplate accepting ConnectionFactory.
This is a short-cut constructor that allows for simpler creation of the Template API.
1 parent ce57b25 commit c23485d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplate.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.data.projection.ProjectionInformation;
5252
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
5353
import org.springframework.data.r2dbc.convert.R2dbcConverter;
54+
import org.springframework.data.r2dbc.dialect.DialectResolver;
5455
import org.springframework.data.r2dbc.dialect.R2dbcDialect;
5556
import org.springframework.data.r2dbc.mapping.OutboundRow;
5657
import org.springframework.data.r2dbc.mapping.event.AfterConvertCallback;
@@ -101,6 +102,25 @@ public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAw
101102

102103
private @Nullable ReactiveEntityCallbacks entityCallbacks;
103104

105+
/**
106+
* Create a new {@link R2dbcEntityTemplate} given {@link ConnectionFactory}.
107+
*
108+
* @param connectionFactory must not be {@literal null}.
109+
* @since 1.2
110+
*/
111+
public R2dbcEntityTemplate(ConnectionFactory connectionFactory) {
112+
113+
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
114+
115+
R2dbcDialect dialect = DialectResolver.getDialect(connectionFactory);
116+
117+
this.databaseClient = DatabaseClient.builder().connectionFactory(connectionFactory)
118+
.bindMarkers(dialect.getBindMarkersFactory()).build();
119+
this.dataAccessStrategy = new DefaultReactiveDataAccessStrategy(dialect);
120+
this.mappingContext = dataAccessStrategy.getConverter().getMappingContext();
121+
this.projectionFactory = new SpelAwareProxyProjectionFactory();
122+
}
123+
104124
/**
105125
* Create a new {@link R2dbcEntityTemplate} given {@link DatabaseClient}.
106126
*

src/test/java/org/springframework/data/r2dbc/documentation/R2dbcEntityTemplateSnippets.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void insert(R2dbcEntityTemplate template) {
7676

7777
// tag::insert[]
7878
Mono<Person> insert = template.insert(Person.class) // <1>
79-
.using(new Person()); // <2>
79+
.using(new Person("John", "Doe")); // <2>
8080
// end::insert[]
8181
}
8282

0 commit comments

Comments
 (0)