1515 */
1616package org .springframework .data .jdbc .core ;
1717
18+ import lombok .NonNull ;
19+ import lombok .RequiredArgsConstructor ;
20+
1821import java .util .HashMap ;
1922import java .util .Map ;
2023import java .util .Optional ;
4649 * @author Jens Schauder
4750 * @since 1.0
4851 */
52+ @ RequiredArgsConstructor
4953public class DefaultDataAccessStrategy implements DataAccessStrategy {
5054
5155 private static final String ENTITY_NEW_AFTER_INSERT = "Entity [%s] still 'new' after insert. Please set either"
5256 + " the id property in a BeforeInsert event handler, or ensure the database creates a value and your "
5357 + "JDBC driver returns it." ;
5458
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 ;
6863
6964 /**
7065 * Creates a {@link DefaultDataAccessStrategy} which references it self for resolution of recursive data accesses.
7166 * Only suitable if this is the only access strategy in use.
7267 */
73- public DefaultDataAccessStrategy (SqlGeneratorSource sqlGeneratorSource , JdbcMappingContext context ) {
68+ public DefaultDataAccessStrategy (SqlGeneratorSource sqlGeneratorSource , JdbcMappingContext context ,
69+ NamedParameterJdbcOperations operations ) {
7470
7571 this .sqlGeneratorSource = sqlGeneratorSource ;
76- this .operations = context . getTemplate () ;
72+ this .operations = operations ;
7773 this .context = context ;
7874 this .accessStrategy = this ;
7975 }
@@ -110,9 +106,12 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
110106 if (idProperty != null && idValue == null && entityInformation .isNew (instance )) {
111107 throw new IllegalStateException (String .format (ENTITY_NEW_AFTER_INSERT , persistentEntity ));
112108 }
113-
114109 }
115110
111+ /*
112+ * (non-Javadoc)
113+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#update(java.lang.Object, java.lang.Class)
114+ */
116115 @ Override
117116 public <S > void update (S instance , Class <S > domainType ) {
118117
@@ -121,6 +120,10 @@ public <S> void update(S instance, Class<S> domainType) {
121120 operations .update (sql (domainType ).getUpdate (), getPropertyMap (instance , persistentEntity ));
122121 }
123122
123+ /*
124+ * (non-Javadoc)
125+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, java.lang.Class)
126+ */
124127 @ Override
125128 public void delete (Object id , Class <?> domainType ) {
126129
@@ -130,6 +133,10 @@ public void delete(Object id, Class<?> domainType) {
130133 operations .update (deleteByIdSql , parameter );
131134 }
132135
136+ /*
137+ * (non-Javadoc)
138+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, org.springframework.data.mapping.PropertyPath)
139+ */
133140 @ Override
134141 public void delete (Object rootId , PropertyPath propertyPath ) {
135142
@@ -145,39 +152,63 @@ public void delete(Object rootId, PropertyPath propertyPath) {
145152 operations .update (format , parameters );
146153 }
147154
155+ /*
156+ * (non-Javadoc)
157+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(java.lang.Class)
158+ */
148159 @ Override
149160 public <T > void deleteAll (Class <T > domainType ) {
150161 operations .getJdbcOperations ().update (sql (domainType ).createDeleteAllSql (null ));
151162 }
152163
164+ /*
165+ * (non-Javadoc)
166+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(org.springframework.data.mapping.PropertyPath)
167+ */
153168 @ Override
154169 public <T > void deleteAll (PropertyPath propertyPath ) {
155170 operations .getJdbcOperations ().update (sql (propertyPath .getOwningType ().getType ()).createDeleteAllSql (propertyPath ));
156171 }
157172
158- @ SuppressWarnings ("ConstantConditions" )
173+ /*
174+ * (non-Javadoc)
175+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#count(java.lang.Class)
176+ */
159177 @ Override
160178 public long count (Class <?> domainType ) {
161179 return operations .getJdbcOperations ().queryForObject (sql (domainType ).getCount (), Long .class );
162180 }
163181
182+ /*
183+ * (non-Javadoc)
184+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#findById(java.lang.Object, java.lang.Class)
185+ */
164186 @ Override
165187 public <T > T findById (Object id , Class <T > domainType ) {
166188
167189 String findOneSql = sql (domainType ).getFindOne ();
168190 MapSqlParameterSource parameter = createIdParameterSource (id , domainType );
191+
169192 try {
170193 return operations .queryForObject (findOneSql , parameter , getEntityRowMapper (domainType ));
171194 } catch (EmptyResultDataAccessException e ) {
172195 return null ;
173196 }
174197 }
175198
199+ /*
200+ * (non-Javadoc)
201+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAll(java.lang.Class)
202+ */
176203 @ Override
177204 public <T > Iterable <T > findAll (Class <T > domainType ) {
178205 return operations .query (sql (domainType ).getFindAll (), getEntityRowMapper (domainType ));
179206 }
180207
208+ /*
209+ * (non-Javadoc)
210+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllById(java.lang.Iterable, java.lang.Class)
211+ */
181212 @ Override
182213 public <T > Iterable <T > findAllById (Iterable <?> ids , Class <T > domainType ) {
183214
@@ -194,15 +225,19 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
194225 return operations .query (findAllInListSql , parameter , getEntityRowMapper (domainType ));
195226 }
196227
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+ */
198232 @ Override
233+ @ SuppressWarnings ("unchecked" )
199234 public <T > Iterable <T > findAllByProperty (Object rootId , JdbcPersistentProperty property ) {
200235
201236 Assert .notNull (rootId , "rootId must not be null." );
202237
203238 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 ());
206241
207242 MapSqlParameterSource parameter = new MapSqlParameterSource (property .getReverseColumnName (), rootId );
208243
@@ -211,11 +246,16 @@ public <T> Iterable<T> findAllByProperty(Object rootId, JdbcPersistentProperty p
211246 : getEntityRowMapper (actualType ));
212247 }
213248
249+ /*
250+ * (non-Javadoc)
251+ * @see org.springframework.data.jdbc.core.DataAccessStrategy#existsById(java.lang.Object, java.lang.Class)
252+ */
214253 @ Override
215254 public <T > boolean existsById (Object id , Class <T > domainType ) {
216255
217256 String existsSql = sql (domainType ).getExists ();
218257 MapSqlParameterSource parameter = createIdParameterSource (id , domainType );
258+
219259 return operations .queryForObject (existsSql , parameter , Boolean .class );
220260 }
221261
0 commit comments