26
26
import org .apache .commons .logging .Log ;
27
27
import org .apache .commons .logging .LogFactory ;
28
28
import org .springframework .beans .BeansException ;
29
- import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
30
29
import org .springframework .context .ApplicationContext ;
31
30
import org .springframework .context .ApplicationContextAware ;
32
31
import org .springframework .context .annotation .Bean ;
40
39
import org .springframework .data .jdbc .core .dialect .JdbcDialect ;
41
40
import org .springframework .data .jdbc .core .mapping .JdbcMappingContext ;
42
41
import org .springframework .data .jdbc .core .mapping .JdbcSimpleTypes ;
42
+ import org .springframework .data .jdbc .dialect .DialectResolver ;
43
43
import org .springframework .data .mapping .model .SimpleTypeHolder ;
44
44
import org .springframework .data .relational .RelationalManagedTypes ;
45
45
import org .springframework .data .relational .core .conversion .RelationalConverter ;
61
61
* @author Christoph Strobl
62
62
* @author Myeonghyeon Lee
63
63
* @author Chirag Tailor
64
+ * @author Tomohiko Ozawa
64
65
* @since 1.1
65
66
*/
66
67
@ Configuration (proxyBeanMethods = false )
@@ -103,7 +104,7 @@ public RelationalManagedTypes jdbcManagedTypes() throws ClassNotFoundException {
103
104
*
104
105
* @param namingStrategy optional {@link NamingStrategy}. Use
105
106
* {@link org.springframework.data.relational.core.mapping.DefaultNamingStrategy#INSTANCE} as fallback.
106
- * @param customConversions see {@link #jdbcCustomConversions()}.
107
+ * @param customConversions see {@link #jdbcCustomConversions(Optional )}.
107
108
* @param jdbcManagedTypes JDBC managed types, typically discovered through {@link #jdbcManagedTypes() an entity
108
109
* scan}.
109
110
* @return must not be {@literal null}.
@@ -124,7 +125,7 @@ public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStra
124
125
* {@link #jdbcMappingContext(Optional, JdbcCustomConversions, RelationalManagedTypes)}.
125
126
*
126
127
* @see #jdbcMappingContext(Optional, JdbcCustomConversions, RelationalManagedTypes)
127
- * @see #jdbcCustomConversions()
128
+ * @see #jdbcCustomConversions(Optional )
128
129
* @return must not be {@literal null}.
129
130
*/
130
131
@ Bean
@@ -147,23 +148,17 @@ public JdbcConverter jdbcConverter(JdbcMappingContext mappingContext, NamedParam
147
148
* @return will never be {@literal null}.
148
149
*/
149
150
@ Bean
150
- public JdbcCustomConversions jdbcCustomConversions () {
151
-
152
- try {
153
-
154
- Dialect dialect = applicationContext .getBean (Dialect .class );
155
- SimpleTypeHolder simpleTypeHolder = dialect .simpleTypes ().isEmpty () ? JdbcSimpleTypes .HOLDER
156
- : new SimpleTypeHolder (dialect .simpleTypes (), JdbcSimpleTypes .HOLDER );
157
-
158
- return new JdbcCustomConversions (
159
- CustomConversions .StoreConversions .of (simpleTypeHolder , storeConverters (dialect )), userConverters ());
160
-
161
- } catch (NoSuchBeanDefinitionException exception ) {
162
-
151
+ public JdbcCustomConversions jdbcCustomConversions (Optional <Dialect > dialect ) {
152
+ return dialect .map (d -> {
153
+ SimpleTypeHolder simpleTypeHolder = d .simpleTypes ().isEmpty ()
154
+ ? JdbcSimpleTypes .HOLDER
155
+ : new SimpleTypeHolder (d .simpleTypes (), JdbcSimpleTypes .HOLDER );
156
+ return new JdbcCustomConversions (CustomConversions .StoreConversions .of (simpleTypeHolder , storeConverters (d )),
157
+ userConverters ());
158
+ }).orElseGet (() -> {
163
159
LOG .warn ("No dialect found; CustomConversions will be configured without dialect specific conversions" );
164
-
165
160
return new JdbcCustomConversions ();
166
- }
161
+ });
167
162
}
168
163
169
164
protected List <?> userConverters () {
@@ -191,7 +186,7 @@ private List<Object> storeConverters(Dialect dialect) {
191
186
public JdbcAggregateTemplate jdbcAggregateTemplate (ApplicationContext applicationContext ,
192
187
JdbcMappingContext mappingContext , JdbcConverter converter , DataAccessStrategy dataAccessStrategy ) {
193
188
194
- return new JdbcAggregateTemplate (applicationContext , mappingContext , converter , dataAccessStrategy );
189
+ return new JdbcAggregateTemplate (applicationContext , converter , dataAccessStrategy );
195
190
}
196
191
197
192
/**
@@ -207,8 +202,7 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
207
202
208
203
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource (context , jdbcConverter , dialect );
209
204
DataAccessStrategyFactory factory = new DataAccessStrategyFactory (sqlGeneratorSource , jdbcConverter , operations ,
210
- new SqlParametersFactory (context , jdbcConverter ),
211
- new InsertStrategyFactory (operations , dialect ));
205
+ new SqlParametersFactory (context , jdbcConverter ), new InsertStrategyFactory (operations , dialect ));
212
206
213
207
return factory .create ();
214
208
}
@@ -219,8 +213,7 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
219
213
* @param operations the {@link NamedParameterJdbcOperations} allowing access to a {@link java.sql.Connection}.
220
214
* @return the {@link Dialect} to be used.
221
215
* @since 2.0
222
- * @throws org.springframework.data.jdbc.repository.config.DialectResolver.NoDialectException if the {@link Dialect}
223
- * cannot be determined.
216
+ * @throws DialectResolver.NoDialectException if the {@link Dialect} cannot be determined.
224
217
*/
225
218
@ Bean
226
219
public Dialect jdbcDialect (NamedParameterJdbcOperations operations ) {
0 commit comments