Skip to content

Commit f43f054

Browse files
committed
DATAJDBC-359 - Polishing.
Deprecate remaining DataAccessStrategy types in jdbc.core and create replacements in jdbc.core.convert. Migrate using code to replacement types. Simplify warnings and if flows. Add since version to deprecations. Move MyBatisDataAccessStrategyUnitTests from core to mybatis package. Original pull request: #150.
1 parent b1c68d9 commit f43f054

34 files changed

+634
-528
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/CascadingDataAccessStrategy.java

Lines changed: 7 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -15,158 +15,24 @@
1515
*/
1616
package org.springframework.data.jdbc.core;
1717

18-
import java.util.ArrayList;
1918
import java.util.List;
20-
import java.util.Map;
21-
import java.util.function.Consumer;
22-
import java.util.function.Function;
2319

24-
import org.springframework.data.relational.domain.Identifier;
25-
import org.springframework.data.mapping.PersistentPropertyPath;
26-
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
20+
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2721

2822
/**
2923
* Delegates each methods to the {@link DataAccessStrategy}s passed to the constructor in turn until the first that does
3024
* not throw an exception.
3125
*
3226
* @author Jens Schauder
27+
* @author Mark Paluch
28+
* @deprecated since 1.1, use {@link org.springframework.data.jdbc.core.convert.CascadingDataAccessStrategy}
3329
*/
34-
public class CascadingDataAccessStrategy implements DataAccessStrategy {
35-
36-
private final List<DataAccessStrategy> strategies;
30+
@Deprecated
31+
public class CascadingDataAccessStrategy
32+
extends org.springframework.data.jdbc.core.convert.CascadingDataAccessStrategy {
3733

3834
public CascadingDataAccessStrategy(List<DataAccessStrategy> strategies) {
39-
this.strategies = new ArrayList<>(strategies);
40-
}
41-
42-
/*
43-
* (non-Javadoc)
44-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
45-
*/
46-
@Override
47-
public <T> Object insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) {
48-
return collect(das -> das.insert(instance, domainType, additionalParameters));
49-
}
50-
51-
/*
52-
* (non-Javadoc)
53-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, org.springframework.data.jdbc.core.ParentKeys)
54-
*/
55-
@Override
56-
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier) {
57-
return collect(das -> das.insert(instance, domainType, identifier));
58-
}
59-
60-
/*
61-
* (non-Javadoc)
62-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#update(java.lang.Object, java.lang.Class)
63-
*/
64-
@Override
65-
public <S> boolean update(S instance, Class<S> domainType) {
66-
return collect(das -> das.update(instance, domainType));
67-
}
68-
69-
/*
70-
* (non-Javadoc)
71-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, java.lang.Class)
72-
*/
73-
@Override
74-
public void delete(Object id, Class<?> domainType) {
75-
collectVoid(das -> das.delete(id, domainType));
76-
}
77-
78-
/*
79-
* (non-Javadoc)
80-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, org.springframework.data.mapping.PersistentPropertyPath)
81-
*/
82-
@Override
83-
public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentProperty> propertyPath) {
84-
collectVoid(das -> das.delete(rootId, propertyPath));
85-
}
86-
87-
/*
88-
* (non-Javadoc)
89-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(java.lang.Class)
90-
*/
91-
@Override
92-
public <T> void deleteAll(Class<T> domainType) {
93-
collectVoid(das -> das.deleteAll(domainType));
94-
}
95-
96-
/*
97-
* (non-Javadoc)
98-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(org.springframework.data.mapping.PersistentPropertyPath)
99-
*/
100-
@Override
101-
public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> propertyPath) {
102-
collectVoid(das -> das.deleteAll(propertyPath));
103-
}
104-
105-
/*
106-
* (non-Javadoc)
107-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#count(java.lang.Class)
108-
*/
109-
@Override
110-
public long count(Class<?> domainType) {
111-
return collect(das -> das.count(domainType));
112-
}
113-
114-
/*
115-
* (non-Javadoc)
116-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findById(java.lang.Object, java.lang.Class)
117-
*/
118-
@Override
119-
public <T> T findById(Object id, Class<T> domainType) {
120-
return collect(das -> das.findById(id, domainType));
121-
}
122-
123-
/*
124-
* (non-Javadoc)
125-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findAll(java.lang.Class)
126-
*/
127-
@Override
128-
public <T> Iterable<T> findAll(Class<T> domainType) {
129-
return collect(das -> das.findAll(domainType));
130-
}
131-
132-
/*
133-
* (non-Javadoc)
134-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllById(java.lang.Iterable, java.lang.Class)
135-
*/
136-
@Override
137-
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
138-
return collect(das -> das.findAllById(ids, domainType));
139-
}
140-
141-
/*
142-
* (non-Javadoc)
143-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllByProperty(java.lang.Object, org.springframework.data.relational.core.mapping.RelationalPersistentProperty)
144-
*/
145-
@Override
146-
public <T> Iterable<T> findAllByProperty(Object rootId, RelationalPersistentProperty property) {
147-
return collect(das -> das.findAllByProperty(rootId, property));
148-
}
149-
150-
/*
151-
* (non-Javadoc)
152-
* @see org.springframework.data.jdbc.core.DataAccessStrategy#existsById(java.lang.Object, java.lang.Class)
153-
*/
154-
@Override
155-
public <T> boolean existsById(Object id, Class<T> domainType) {
156-
return collect(das -> das.existsById(id, domainType));
157-
}
158-
159-
private <T> T collect(Function<DataAccessStrategy, T> function) {
160-
161-
// Keep <T> as Eclipse fails to compile if <> is used.
162-
return strategies.stream().collect(new FunctionCollector<>(function));
35+
super(strategies);
16336
}
16437

165-
private void collectVoid(Consumer<DataAccessStrategy> consumer) {
166-
167-
collect(das -> {
168-
consumer.accept(das);
169-
return null;
170-
});
171-
}
17238
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DataAccessStrategy.java

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -15,149 +15,14 @@
1515
*/
1616
package org.springframework.data.jdbc.core;
1717

18-
import java.util.Map;
19-
20-
import org.springframework.data.mapping.PersistentPropertyPath;
21-
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
22-
import org.springframework.data.relational.domain.Identifier;
23-
import org.springframework.lang.Nullable;
24-
2518
/**
2619
* Abstraction for accesses to the database that should be implementable with a single SQL statement per method and
2720
* relates to a single entity as opposed to {@link JdbcAggregateOperations} which provides interactions related to
2821
* complete aggregates.
2922
*
3023
* @author Jens Schauder
24+
* @author Mark Paluch
25+
* @deprecated since 1.1, use {@link org.springframework.data.jdbc.core.convert.DataAccessStrategy}
3126
*/
32-
public interface DataAccessStrategy {
33-
34-
/**
35-
* Inserts a the data of a single entity. Referenced entities don't get handled.
36-
*
37-
* @param instance the instance to be stored. Must not be {@code null}.
38-
* @param domainType the type of the instance. Must not be {@code null}.
39-
* @param additionalParameters name-value pairs of additional parameters. Especially ids of parent entities that need
40-
* to get referenced are contained in this map. Must not be {@code null}.
41-
* @param <T> the type of the instance.
42-
* @return the id generated by the database if any.
43-
* @deprecated since 1.1, use {@link #insert(Object, Class, Identifier)} instead.
44-
*/
45-
@Deprecated
46-
<T> Object insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters);
47-
48-
/**
49-
* Inserts a the data of a single entity. Referenced entities don't get handled.
50-
*
51-
* @param instance the instance to be stored. Must not be {@code null}.
52-
* @param domainType the type of the instance. Must not be {@code null}.
53-
* @param identifier information about data that needs to be considered for the insert but which is not part of the
54-
* entity. Namely references back to a parent entity and key/index columns for entities that are stored in a
55-
* {@link Map} or {@link java.util.List}.
56-
* @param <T> the type of the instance.
57-
* @return the id generated by the database if any.
58-
* @since 1.1
59-
*/
60-
default <T> Object insert(T instance, Class<T> domainType, Identifier identifier){
61-
return insert(instance, domainType, identifier.toMap());
62-
}
63-
64-
/**
65-
* Updates the data of a single entity in the database. Referenced entities don't get handled.
66-
*
67-
* @param instance the instance to save. Must not be {@code null}.
68-
* @param domainType the type of the instance to save. Must not be {@code null}.
69-
* @param <T> the type of the instance to save.
70-
* @return whether the update actually updated a row.
71-
*/
72-
<T> boolean update(T instance, Class<T> domainType);
73-
74-
/**
75-
* deletes a single row identified by the id, from the table identified by the domainType. Does not handle cascading
76-
* deletes.
77-
*
78-
* @param id the id of the row to be deleted. Must not be {@code null}.
79-
* @param domainType the type of entity to be deleted. Implicitly determines the table to operate on. Must not be
80-
* {@code null}.
81-
*/
82-
void delete(Object id, Class<?> domainType);
83-
84-
/**
85-
* Deletes all entities reachable via {@literal propertyPath} from the instance identified by {@literal rootId}.
86-
*
87-
* @param rootId Id of the root object on which the {@literal propertyPath} is based. Must not be {@code null}.
88-
* @param propertyPath Leading from the root object to the entities to be deleted. Must not be {@code null}.
89-
*/
90-
void delete(Object rootId, PersistentPropertyPath<RelationalPersistentProperty> propertyPath);
91-
92-
/**
93-
* Deletes all entities of the given domain type.
94-
*
95-
* @param domainType the domain type for which to delete all entries. Must not be {@code null}.
96-
* @param <T> type of the domain type.
97-
*/
98-
<T> void deleteAll(Class<T> domainType);
99-
100-
/**
101-
* Deletes all entities reachable via {@literal propertyPath} from any instance.
102-
*
103-
* @param propertyPath Leading from the root object to the entities to be deleted. Must not be {@code null}.
104-
*/
105-
void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> propertyPath);
106-
107-
/**
108-
* Counts the rows in the table representing the given domain type.
109-
*
110-
* @param domainType the domain type for which to count the elements. Must not be {@code null}.
111-
* @return the count. Guaranteed to be not {@code null}.
112-
*/
113-
long count(Class<?> domainType);
114-
115-
/**
116-
* Loads a single entity identified by type and id.
117-
*
118-
* @param id the id of the entity to load. Must not be {@code null}.
119-
* @param domainType the domain type of the entity. Must not be {@code null}.
120-
* @param <T> the type of the entity.
121-
* @return Might return {@code null}.
122-
*/
123-
@Nullable
124-
<T> T findById(Object id, Class<T> domainType);
125-
126-
/**
127-
* Loads all entities of the given type.
128-
*
129-
* @param domainType the type of entities to load. Must not be {@code null}.
130-
* @param <T> the type of entities to load.
131-
* @return Guaranteed to be not {@code null}.
132-
*/
133-
<T> Iterable<T> findAll(Class<T> domainType);
134-
135-
/**
136-
* Loads all entities that match one of the ids passed as an argument. It is not guaranteed that the number of ids
137-
* passed in matches the number of entities returned.
138-
*
139-
* @param ids the Ids of the entities to load. Must not be {@code null}.
140-
* @param domainType the type of entities to laod. Must not be {@code null}.
141-
* @param <T> type of entities to load.
142-
* @return the loaded entities. Guaranteed to be not {@code null}.
143-
*/
144-
<T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType);
145-
146-
/**
147-
* Finds all entities reachable via {@literal property} from the instance identified by {@literal rootId}.
148-
*
149-
* @param rootId Id of the root object on which the {@literal propertyPath} is based.
150-
* @param property Leading from the root object to the entities to be found.
151-
*/
152-
<T> Iterable<T> findAllByProperty(Object rootId, RelationalPersistentProperty property);
153-
154-
/**
155-
* returns if a row with the given id exists for the given type.
156-
*
157-
* @param id the id of the entity for which to check. Must not be {@code null}.
158-
* @param domainType the type of the entity to check for. Must not be {@code null}.
159-
* @param <T> the type of the entity.
160-
* @return {@code true} if a matching row exists, otherwise {@code false}.
161-
*/
162-
<T> boolean existsById(Object id, Class<T> domainType);
163-
}
27+
@Deprecated
28+
public interface DataAccessStrategy extends org.springframework.data.jdbc.core.convert.DataAccessStrategy {}
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2017-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,32 +15,33 @@
1515
*/
1616
package org.springframework.data.jdbc.core;
1717

18+
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
1819
import org.springframework.data.jdbc.core.convert.JdbcConverter;
1920
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
2021
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
2122
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
2223

2324
/**
2425
* The default {@link DataAccessStrategy} is to generate SQL statements based on meta data from the entity.
25-
*
26+
*
2627
* @author Jens Schauder
27-
* @deprecated Use {@link org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy} instead.
28+
* @deprecated since 1.1, use {@link org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy} instead.
2829
*/
2930
@Deprecated
3031
public class DefaultDataAccessStrategy extends org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy {
3132

3233
/**
33-
* Creates a {@link org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy} which references it self for resolution of recursive data accesses.
34-
* Only suitable if this is the only access strategy in use.
35-
*
36-
* @param sqlGeneratorSource must not be {@literal null}.
37-
* @param context must not be {@literal null}.
38-
* @param converter must not be {@literal null}.
39-
* @param operations must not be {@literal null}.
40-
*/
41-
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
42-
JdbcConverter converter, NamedParameterJdbcOperations operations) {
43-
super(sqlGeneratorSource, context, converter, operations);
44-
}
34+
* Creates a {@link org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy} which references it self for
35+
* resolution of recursive data accesses. Only suitable if this is the only access strategy in use.
36+
*
37+
* @param sqlGeneratorSource must not be {@literal null}.
38+
* @param context must not be {@literal null}.
39+
* @param converter must not be {@literal null}.
40+
* @param operations must not be {@literal null}.
41+
*/
42+
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
43+
JdbcConverter converter, NamedParameterJdbcOperations operations) {
44+
super(sqlGeneratorSource, context, converter, operations);
45+
}
4546

4647
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.Map;
2222

23+
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2324
import org.springframework.data.mapping.PersistentPropertyPath;
2425
import org.springframework.data.relational.core.conversion.DbAction;
2526
import org.springframework.data.relational.core.conversion.DbAction.Delete;

0 commit comments

Comments
 (0)