4141import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
4242import org .springframework .jdbc .support .GeneratedKeyHolder ;
4343import org .springframework .jdbc .support .KeyHolder ;
44+ import org .springframework .lang .Nullable ;
4445import org .springframework .util .Assert ;
4546
4647/**
@@ -88,6 +89,9 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
8889 JdbcPersistentProperty idProperty = persistentEntity .getIdProperty ();
8990
9091 if (idValue != null ) {
92+
93+ Assert .notNull (idProperty , "Since we have a non-null idValue, we must have an idProperty as well." );
94+
9195 additionalParameters .put (idProperty .getColumnName (), convert (idValue , idProperty .getColumnType ()));
9296 }
9397
@@ -167,7 +171,7 @@ public <T> void deleteAll(Class<T> domainType) {
167171 * @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(org.springframework.data.mapping.PropertyPath)
168172 */
169173 @ Override
170- public < T > void deleteAll (PropertyPath propertyPath ) {
174+ public void deleteAll (PropertyPath propertyPath ) {
171175 operations .getJdbcOperations ().update (sql (propertyPath .getOwningType ().getType ()).createDeleteAllSql (propertyPath ));
172176 }
173177
@@ -177,21 +181,27 @@ public <T> void deleteAll(PropertyPath propertyPath) {
177181 */
178182 @ Override
179183 public long count (Class <?> domainType ) {
180- return operations .getJdbcOperations ().queryForObject (sql (domainType ).getCount (), Long .class );
184+
185+ Long result = operations .getJdbcOperations ().queryForObject (sql (domainType ).getCount (), Long .class );
186+
187+ Assert .notNull (result , "The result of a count query must not be null." );
188+
189+ return result ;
181190 }
182191
183192 /*
184193 * (non-Javadoc)
185194 * @see org.springframework.data.jdbc.core.DataAccessStrategy#findById(java.lang.Object, java.lang.Class)
186195 */
196+ @ SuppressWarnings ("unchecked" )
187197 @ Override
188198 public <T > T findById (Object id , Class <T > domainType ) {
189199
190200 String findOneSql = sql (domainType ).getFindOne ();
191201 MapSqlParameterSource parameter = createIdParameterSource (id , domainType );
192202
193203 try {
194- return operations .queryForObject (findOneSql , parameter , getEntityRowMapper (domainType ));
204+ return operations .queryForObject (findOneSql , parameter , ( RowMapper < T >) getEntityRowMapper (domainType ));
195205 } catch (EmptyResultDataAccessException e ) {
196206 return null ;
197207 }
@@ -201,15 +211,17 @@ public <T> T findById(Object id, Class<T> domainType) {
201211 * (non-Javadoc)
202212 * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAll(java.lang.Class)
203213 */
214+ @ SuppressWarnings ("unchecked" )
204215 @ Override
205216 public <T > Iterable <T > findAll (Class <T > domainType ) {
206- return operations .query (sql (domainType ).getFindAll (), getEntityRowMapper (domainType ));
217+ return operations .query (sql (domainType ).getFindAll (), ( RowMapper < T >) getEntityRowMapper (domainType ));
207218 }
208219
209220 /*
210221 * (non-Javadoc)
211222 * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllById(java.lang.Iterable, java.lang.Class)
212223 */
224+ @ SuppressWarnings ("unchecked" )
213225 @ Override
214226 public <T > Iterable <T > findAllById (Iterable <?> ids , Class <T > domainType ) {
215227
@@ -223,7 +235,7 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
223235 .collect (Collectors .toList ()) //
224236 );
225237
226- return operations .query (findAllInListSql , parameter , getEntityRowMapper (domainType ));
238+ return operations .query (findAllInListSql , parameter , ( RowMapper < T >) getEntityRowMapper (domainType ));
227239 }
228240
229241 /*
@@ -242,9 +254,10 @@ public <T> Iterable<T> findAllByProperty(Object rootId, JdbcPersistentProperty p
242254
243255 MapSqlParameterSource parameter = new MapSqlParameterSource (property .getReverseColumnName (), rootId );
244256
245- return (Iterable <T >) operations .query (findAllByProperty , parameter , property .isMap () //
246- ? getMapEntityRowMapper (property ) //
247- : getEntityRowMapper (actualType ));
257+ return operations .query (findAllByProperty , parameter , //
258+ (RowMapper <T >) (property .isMap () //
259+ ? this .getMapEntityRowMapper (property ) //
260+ : this .getEntityRowMapper (actualType )));
248261 }
249262
250263 /*
@@ -257,7 +270,11 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
257270 String existsSql = sql (domainType ).getExists ();
258271 MapSqlParameterSource parameter = createIdParameterSource (id , domainType );
259272
260- return operations .queryForObject (existsSql , parameter , Boolean .class );
273+ Boolean result = operations .queryForObject (existsSql , parameter , Boolean .class );
274+
275+ Assert .notNull (result , "The result of an exists query must not be null" );
276+
277+ return result ;
261278 }
262279
263280 private <S > MapSqlParameterSource getPropertyMap (final S instance , JdbcPersistentEntity <S > persistentEntity ) {
@@ -277,14 +294,16 @@ private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersisten
277294 }
278295
279296 @ SuppressWarnings ("unchecked" )
297+ @ Nullable
280298 private <S , ID > ID getIdValueOrNull (S instance , JdbcPersistentEntity <S > persistentEntity ) {
281299
282300 ID idValue = (ID ) persistentEntity .getIdentifierAccessor (instance ).getIdentifier ();
283301
284302 return isIdPropertyNullOrScalarZero (idValue , persistentEntity ) ? null : idValue ;
285303 }
286304
287- private static <S , ID > boolean isIdPropertyNullOrScalarZero (ID idValue , JdbcPersistentEntity <S > persistentEntity ) {
305+ private static <S , ID > boolean isIdPropertyNullOrScalarZero (@ Nullable ID idValue ,
306+ JdbcPersistentEntity <S > persistentEntity ) {
288307
289308 JdbcPersistentProperty idProperty = persistentEntity .getIdProperty ();
290309 return idValue == null //
@@ -319,30 +338,38 @@ private <S> Optional<Object> getIdFromHolder(KeyHolder holder, JdbcPersistentEnt
319338 return Optional .ofNullable (holder .getKey ());
320339 } catch (InvalidDataAccessApiUsageException e ) {
321340 // Postgres returns a value for each column
322- return Optional .ofNullable (holder .getKeys ().get (persistentEntity .getIdColumn ()));
341+ Map <String , Object > keys = holder .getKeys ();
342+ return Optional .ofNullable (keys == null ? null : keys .get (persistentEntity .getIdColumn ()));
323343 }
324344 }
325345
326- public < T > EntityRowMapper <T > getEntityRowMapper (Class <T > domainType ) {
346+ public EntityRowMapper <? > getEntityRowMapper (Class <? > domainType ) {
327347 return new EntityRowMapper <>(getRequiredPersistentEntity (domainType ), context , instantiators , accessStrategy );
328348 }
329349
330- private RowMapper getMapEntityRowMapper (JdbcPersistentProperty property ) {
331- return new MapEntityRowMapper (getEntityRowMapper (property .getActualType ()), property .getKeyColumn ());
350+ @ SuppressWarnings ("unchecked" )
351+ private RowMapper <?> getMapEntityRowMapper (JdbcPersistentProperty property ) {
352+
353+ String keyColumn = property .getKeyColumn ();
354+
355+ Assert .notNull (keyColumn , () -> "keyColumn must not be null for " + property );
356+
357+ return new MapEntityRowMapper <>(getEntityRowMapper (property .getActualType ()), keyColumn );
332358 }
333359
334360 private <T > MapSqlParameterSource createIdParameterSource (Object id , Class <T > domainType ) {
335361
336- return new MapSqlParameterSource ( "id" ,
337- convert ( id , getRequiredPersistentEntity ( domainType ). getRequiredIdProperty (). getColumnType () ));
362+ Class <?> columnType = getRequiredPersistentEntity ( domainType ). getRequiredIdProperty (). getColumnType ();
363+ return new MapSqlParameterSource ( "id" , convert ( id , columnType ));
338364 }
339365
340366 @ SuppressWarnings ("unchecked" )
341367 private <S > JdbcPersistentEntity <S > getRequiredPersistentEntity (Class <S > domainType ) {
342368 return (JdbcPersistentEntity <S >) context .getRequiredPersistentEntity (domainType );
343369 }
344370
345- private <V > V convert (Object from , Class <V > to ) {
371+ @ Nullable
372+ private <V > V convert (@ Nullable Object from , Class <V > to ) {
346373
347374 if (from == null ) {
348375 return null ;
0 commit comments