15
15
*/
16
16
package org .springframework .data .jdbc .core ;
17
17
18
+ import lombok .NonNull ;
19
+ import lombok .RequiredArgsConstructor ;
20
+
18
21
import java .util .HashMap ;
19
22
import java .util .Map ;
20
23
import java .util .Optional ;
46
49
* @author Jens Schauder
47
50
* @since 1.0
48
51
*/
52
+ @ RequiredArgsConstructor
49
53
public class DefaultDataAccessStrategy implements DataAccessStrategy {
50
54
51
55
private static final String ENTITY_NEW_AFTER_INSERT = "Entity [%s] still 'new' after insert. Please set either"
52
56
+ " the id property in a BeforeInsert event handler, or ensure the database creates a value and your "
53
57
+ "JDBC driver returns it." ;
54
58
55
- private final SqlGeneratorSource sqlGeneratorSource ;
56
- private final NamedParameterJdbcOperations operations ;
57
- private final JdbcMappingContext context ;
58
- private final DataAccessStrategy accessStrategy ;
59
-
60
- public DefaultDataAccessStrategy (SqlGeneratorSource sqlGeneratorSource , JdbcMappingContext context ,
61
- DataAccessStrategy accessStrategy ) {
62
-
63
- this .sqlGeneratorSource = sqlGeneratorSource ;
64
- this .operations = context .getTemplate ();
65
- this .context = context ;
66
- this .accessStrategy = accessStrategy ;
67
- }
59
+ private final @ NonNull SqlGeneratorSource sqlGeneratorSource ;
60
+ private final @ NonNull JdbcMappingContext context ;
61
+ private final @ NonNull DataAccessStrategy accessStrategy ;
62
+ private final @ NonNull NamedParameterJdbcOperations operations ;
68
63
69
64
/**
70
65
* Creates a {@link DefaultDataAccessStrategy} which references it self for resolution of recursive data accesses.
71
66
* Only suitable if this is the only access strategy in use.
72
67
*/
73
- public DefaultDataAccessStrategy (SqlGeneratorSource sqlGeneratorSource , JdbcMappingContext context ) {
68
+ public DefaultDataAccessStrategy (SqlGeneratorSource sqlGeneratorSource , JdbcMappingContext context ,
69
+ NamedParameterJdbcOperations operations ) {
74
70
75
71
this .sqlGeneratorSource = sqlGeneratorSource ;
76
- this .operations = context . getTemplate () ;
72
+ this .operations = operations ;
77
73
this .context = context ;
78
74
this .accessStrategy = this ;
79
75
}
@@ -110,9 +106,12 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
110
106
if (idProperty != null && idValue == null && entityInformation .isNew (instance )) {
111
107
throw new IllegalStateException (String .format (ENTITY_NEW_AFTER_INSERT , persistentEntity ));
112
108
}
113
-
114
109
}
115
110
111
+ /*
112
+ * (non-Javadoc)
113
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#update(java.lang.Object, java.lang.Class)
114
+ */
116
115
@ Override
117
116
public <S > void update (S instance , Class <S > domainType ) {
118
117
@@ -121,6 +120,10 @@ public <S> void update(S instance, Class<S> domainType) {
121
120
operations .update (sql (domainType ).getUpdate (), getPropertyMap (instance , persistentEntity ));
122
121
}
123
122
123
+ /*
124
+ * (non-Javadoc)
125
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, java.lang.Class)
126
+ */
124
127
@ Override
125
128
public void delete (Object id , Class <?> domainType ) {
126
129
@@ -130,6 +133,10 @@ public void delete(Object id, Class<?> domainType) {
130
133
operations .update (deleteByIdSql , parameter );
131
134
}
132
135
136
+ /*
137
+ * (non-Javadoc)
138
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, org.springframework.data.mapping.PropertyPath)
139
+ */
133
140
@ Override
134
141
public void delete (Object rootId , PropertyPath propertyPath ) {
135
142
@@ -145,39 +152,63 @@ public void delete(Object rootId, PropertyPath propertyPath) {
145
152
operations .update (format , parameters );
146
153
}
147
154
155
+ /*
156
+ * (non-Javadoc)
157
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(java.lang.Class)
158
+ */
148
159
@ Override
149
160
public <T > void deleteAll (Class <T > domainType ) {
150
161
operations .getJdbcOperations ().update (sql (domainType ).createDeleteAllSql (null ));
151
162
}
152
163
164
+ /*
165
+ * (non-Javadoc)
166
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(org.springframework.data.mapping.PropertyPath)
167
+ */
153
168
@ Override
154
169
public <T > void deleteAll (PropertyPath propertyPath ) {
155
170
operations .getJdbcOperations ().update (sql (propertyPath .getOwningType ().getType ()).createDeleteAllSql (propertyPath ));
156
171
}
157
172
158
- @ SuppressWarnings ("ConstantConditions" )
173
+ /*
174
+ * (non-Javadoc)
175
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#count(java.lang.Class)
176
+ */
159
177
@ Override
160
178
public long count (Class <?> domainType ) {
161
179
return operations .getJdbcOperations ().queryForObject (sql (domainType ).getCount (), Long .class );
162
180
}
163
181
182
+ /*
183
+ * (non-Javadoc)
184
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#findById(java.lang.Object, java.lang.Class)
185
+ */
164
186
@ Override
165
187
public <T > T findById (Object id , Class <T > domainType ) {
166
188
167
189
String findOneSql = sql (domainType ).getFindOne ();
168
190
MapSqlParameterSource parameter = createIdParameterSource (id , domainType );
191
+
169
192
try {
170
193
return operations .queryForObject (findOneSql , parameter , getEntityRowMapper (domainType ));
171
194
} catch (EmptyResultDataAccessException e ) {
172
195
return null ;
173
196
}
174
197
}
175
198
199
+ /*
200
+ * (non-Javadoc)
201
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAll(java.lang.Class)
202
+ */
176
203
@ Override
177
204
public <T > Iterable <T > findAll (Class <T > domainType ) {
178
205
return operations .query (sql (domainType ).getFindAll (), getEntityRowMapper (domainType ));
179
206
}
180
207
208
+ /*
209
+ * (non-Javadoc)
210
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllById(java.lang.Iterable, java.lang.Class)
211
+ */
181
212
@ Override
182
213
public <T > Iterable <T > findAllById (Iterable <?> ids , Class <T > domainType ) {
183
214
@@ -194,15 +225,19 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
194
225
return operations .query (findAllInListSql , parameter , getEntityRowMapper (domainType ));
195
226
}
196
227
197
- @ SuppressWarnings ("unchecked" )
228
+ /*
229
+ * (non-Javadoc)
230
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllByProperty(java.lang.Object, org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)
231
+ */
198
232
@ Override
233
+ @ SuppressWarnings ("unchecked" )
199
234
public <T > Iterable <T > findAllByProperty (Object rootId , JdbcPersistentProperty property ) {
200
235
201
236
Assert .notNull (rootId , "rootId must not be null." );
202
237
203
238
Class <?> actualType = property .getActualType ();
204
- String findAllByProperty = sql (actualType ). getFindAllByProperty ( property . getReverseColumnName (),
205
- property .getKeyColumn (), property .isOrdered ());
239
+ String findAllByProperty = sql (actualType ) //
240
+ . getFindAllByProperty ( property . getReverseColumnName (), property .getKeyColumn (), property .isOrdered ());
206
241
207
242
MapSqlParameterSource parameter = new MapSqlParameterSource (property .getReverseColumnName (), rootId );
208
243
@@ -211,11 +246,16 @@ public <T> Iterable<T> findAllByProperty(Object rootId, JdbcPersistentProperty p
211
246
: getEntityRowMapper (actualType ));
212
247
}
213
248
249
+ /*
250
+ * (non-Javadoc)
251
+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#existsById(java.lang.Object, java.lang.Class)
252
+ */
214
253
@ Override
215
254
public <T > boolean existsById (Object id , Class <T > domainType ) {
216
255
217
256
String existsSql = sql (domainType ).getExists ();
218
257
MapSqlParameterSource parameter = createIdParameterSource (id , domainType );
258
+
219
259
return operations .queryForObject (existsSql , parameter , Boolean .class );
220
260
}
221
261
0 commit comments