48
48
* intermediate {@link RowDocumentResultSetExtractor RowDocument} and mapped via
49
49
* {@link org.springframework.data.relational.core.conversion.RelationalConverter#read(Class, RowDocument)}.
50
50
*
51
- * @param <T> the type of aggregate produced by this reader.
52
51
* @author Jens Schauder
53
52
* @author Mark Paluch
54
53
* @since 3.2
55
54
*/
56
- class AggregateReader < T > implements PathToColumnMapping {
55
+ class AggregateReader implements PathToColumnMapping {
57
56
58
- private final RelationalPersistentEntity <T > aggregate ;
59
- private final Table table ;
57
+ private final AliasFactory aliasFactory ;
60
58
private final SqlGenerator sqlGenerator ;
61
59
private final JdbcConverter converter ;
62
60
private final NamedParameterJdbcOperations jdbcTemplate ;
63
- private final AliasFactory aliasFactory ;
64
61
private final RowDocumentResultSetExtractor extractor ;
65
62
66
- AggregateReader (Dialect dialect , JdbcConverter converter , AliasFactory aliasFactory ,
67
- NamedParameterJdbcOperations jdbcTemplate , RelationalPersistentEntity <T > aggregate ) {
63
+ AggregateReader (Dialect dialect , JdbcConverter converter , NamedParameterJdbcOperations jdbcTemplate ) {
68
64
65
+ this .aliasFactory = new AliasFactory ();
69
66
this .converter = converter ;
70
- this .aggregate = aggregate ;
71
67
this .jdbcTemplate = jdbcTemplate ;
72
- this .table = Table .create (aggregate .getQualifiedTableName ());
73
68
this .sqlGenerator = new SingleQuerySqlGenerator (converter .getMappingContext (), aliasFactory , dialect );
74
- this .aliasFactory = aliasFactory ;
75
69
this .extractor = new RowDocumentResultSetExtractor (converter .getMappingContext (), this );
76
70
}
77
71
@@ -93,54 +87,55 @@ public String keyColumn(AggregatePath path) {
93
87
}
94
88
95
89
@ Nullable
96
- public T findById (Object id ) {
90
+ public < T > T findById (Object id , RelationalPersistentEntity < T > entity ) {
97
91
98
- Query query = Query .query (Criteria .where (aggregate .getRequiredIdProperty ().getName ()).is (id )).limit (1 );
92
+ Query query = Query .query (Criteria .where (entity .getRequiredIdProperty ().getName ()).is (id )).limit (1 );
99
93
100
- return findOne (query );
94
+ return findOne (query , entity );
101
95
}
102
96
103
97
@ Nullable
104
- public T findOne (Query query ) {
105
- return doFind (query , this :: extractZeroOrOne );
98
+ public < T > T findOne (Query query , RelationalPersistentEntity < T > entity ) {
99
+ return doFind (query , entity , rs -> extractZeroOrOne ( rs , entity ) );
106
100
}
107
101
108
- public List <T > findAllById (Iterable <?> ids ) {
102
+ public < T > List <T > findAllById (Iterable <?> ids , RelationalPersistentEntity < T > entity ) {
109
103
110
104
Collection <?> identifiers = ids instanceof Collection <?> idl ? idl : Streamable .of (ids ).toList ();
111
- Query query = Query .query (Criteria .where (aggregate .getRequiredIdProperty ().getName ()).in (identifiers ));
105
+ Query query = Query .query (Criteria .where (entity .getRequiredIdProperty ().getName ()).in (identifiers ));
112
106
113
- return findAll (query );
107
+ return findAll (query , entity );
114
108
}
115
109
116
110
@ SuppressWarnings ("ConstantConditions" )
117
- public List <T > findAll () {
118
- return jdbcTemplate .query (sqlGenerator .findAll (aggregate ), this ::extractAll );
111
+ public <T > List <T > findAll (RelationalPersistentEntity <T > entity ) {
112
+ return jdbcTemplate .query (sqlGenerator .findAll (entity ),
113
+ (ResultSetExtractor <? extends List <T >>) rs -> extractAll (rs , entity ));
119
114
}
120
115
121
- public List <T > findAll (Query query ) {
122
- return doFind (query , this :: extractAll );
116
+ public < T > List <T > findAll (Query query , RelationalPersistentEntity < T > entity ) {
117
+ return doFind (query , entity , rs -> extractAll ( rs , entity ) );
123
118
}
124
119
125
120
@ SuppressWarnings ("ConstantConditions" )
126
- private <R > R doFind (Query query , ResultSetExtractor <R > extractor ) {
121
+ private <T , R > R doFind (Query query , RelationalPersistentEntity < T > entity , ResultSetExtractor <R > extractor ) {
127
122
128
123
MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
129
- Condition condition = createCondition (query , parameterSource );
130
- String sql = sqlGenerator .findAll (aggregate , condition );
124
+ Condition condition = createCondition (query , parameterSource , entity );
125
+ String sql = sqlGenerator .findAll (entity , condition );
131
126
132
127
return jdbcTemplate .query (sql , parameterSource , extractor );
133
128
}
134
129
135
130
@ Nullable
136
- private Condition createCondition (Query query , MapSqlParameterSource parameterSource ) {
131
+ private Condition createCondition (Query query , MapSqlParameterSource parameterSource ,
132
+ RelationalPersistentEntity <?> entity ) {
137
133
138
134
QueryMapper queryMapper = new QueryMapper (converter );
139
135
140
136
Optional <CriteriaDefinition > criteria = query .getCriteria ();
141
- return criteria
142
- .map (criteriaDefinition -> queryMapper .getMappedObject (parameterSource , criteriaDefinition , table , aggregate ))
143
- .orElse (null );
137
+ return criteria .map (criteriaDefinition -> queryMapper .getMappedObject (parameterSource , criteriaDefinition ,
138
+ Table .create (entity .getQualifiedTableName ()), entity )).orElse (null );
144
139
}
145
140
146
141
/**
@@ -152,12 +147,13 @@ private Condition createCondition(Query query, MapSqlParameterSource parameterSo
152
147
* @return a {@code List} of aggregates, fully converted.
153
148
* @throws SQLException on underlying JDBC errors.
154
149
*/
155
- private List <T > extractAll (ResultSet rs ) throws SQLException {
150
+ private < T > List <T > extractAll (ResultSet rs , RelationalPersistentEntity < T > entity ) throws SQLException {
156
151
157
- Iterator <RowDocument > iterate = extractor .iterate (aggregate , rs );
152
+ Iterator <RowDocument > iterate = extractor .iterate (entity , rs );
158
153
List <T > resultList = new ArrayList <>();
154
+
159
155
while (iterate .hasNext ()) {
160
- resultList .add (converter .read (aggregate .getType (), iterate .next ()));
156
+ resultList .add (converter .read (entity .getType (), iterate .next ()));
161
157
}
162
158
163
159
return resultList ;
@@ -175,17 +171,19 @@ private List<T> extractAll(ResultSet rs) throws SQLException {
175
171
* @throws IncorrectResultSizeDataAccessException when the conversion yields more than one instance.
176
172
*/
177
173
@ Nullable
178
- private T extractZeroOrOne (ResultSet rs ) throws SQLException {
174
+ private <T > T extractZeroOrOne (ResultSet rs , RelationalPersistentEntity <T > entity ) throws SQLException {
175
+
176
+ Iterator <RowDocument > iterate = extractor .iterate (entity , rs );
179
177
180
- Iterator <RowDocument > iterate = extractor .iterate (aggregate , rs );
181
178
if (iterate .hasNext ()) {
182
179
183
180
RowDocument object = iterate .next ();
184
181
if (iterate .hasNext ()) {
185
182
throw new IncorrectResultSizeDataAccessException (1 );
186
183
}
187
- return converter .read (aggregate .getType (), object );
184
+ return converter .read (entity .getType (), object );
188
185
}
186
+
189
187
return null ;
190
188
}
191
189
0 commit comments