|
1 | 1 | /*
|
2 |
| - * Copyright 2017-2019 the original author or authors. |
| 2 | + * Copyright 2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.data.jdbc.core;
|
17 | 17 |
|
18 |
| -import java.sql.ResultSet; |
19 |
| -import java.sql.SQLException; |
20 |
| -import java.util.Map; |
21 |
| - |
22 |
| -import org.springframework.core.convert.converter.Converter; |
23 |
| -import org.springframework.data.mapping.MappingException; |
24 |
| -import org.springframework.data.mapping.PersistentEntity; |
25 |
| -import org.springframework.data.mapping.PersistentPropertyAccessor; |
26 |
| -import org.springframework.data.mapping.PreferredConstructor; |
27 |
| -import org.springframework.data.relational.core.conversion.RelationalConverter; |
28 |
| -import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
| 18 | +import org.springframework.data.jdbc.core.convert.JdbcConverter; |
29 | 19 | import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
30 |
| -import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
31 |
| -import org.springframework.jdbc.core.RowMapper; |
32 |
| -import org.springframework.lang.Nullable; |
33 |
| -import org.springframework.util.Assert; |
34 | 20 |
|
35 | 21 | /**
|
36 |
| - * Maps a {@link ResultSet} to an entity of type {@code T}, including entities referenced. This {@link RowMapper} might |
37 |
| - * trigger additional SQL statements in order to load other members of the same aggregate. |
38 |
| - * |
39 | 22 | * @author Jens Schauder
|
40 |
| - * @author Oliver Gierke |
41 |
| - * @author Mark Paluch |
42 |
| - * @author Maciej Walkowiak |
43 |
| - * @author Bastian Wilhelm |
| 23 | + * |
| 24 | + * @deprecated Use {@link org.springframework.data.jdbc.core.convert.EntityRowMapper} instead. |
44 | 25 | */
|
45 |
| -public class EntityRowMapper<T> implements RowMapper<T> { |
46 |
| - |
47 |
| - private static final Converter<Iterable<?>, Map<?, ?>> ITERABLE_OF_ENTRY_TO_MAP_CONVERTER = new IterableOfEntryToMapConverter(); |
48 |
| - |
49 |
| - private final RelationalPersistentEntity<T> entity; |
50 |
| - |
51 |
| - private final RelationalConverter converter; |
52 |
| - private final RelationalMappingContext context; |
53 |
| - private final DataAccessStrategy accessStrategy; |
54 |
| - private final RelationalPersistentProperty idProperty; |
55 |
| - |
56 |
| - public EntityRowMapper(RelationalPersistentEntity<T> entity, RelationalMappingContext context, |
57 |
| - RelationalConverter converter, DataAccessStrategy accessStrategy) { |
58 |
| - |
59 |
| - this.entity = entity; |
60 |
| - this.converter = converter; |
61 |
| - this.context = context; |
62 |
| - this.accessStrategy = accessStrategy; |
63 |
| - this.idProperty = entity.getIdProperty(); |
64 |
| - } |
65 |
| - |
66 |
| - /* |
67 |
| - * (non-Javadoc) |
68 |
| - * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int) |
69 |
| - */ |
70 |
| - @Override |
71 |
| - public T mapRow(ResultSet resultSet, int rowNumber) { |
72 |
| - |
73 |
| - String prefix = ""; |
74 |
| - |
75 |
| - RelationalPersistentProperty idProperty = entity.getIdProperty(); |
76 |
| - |
77 |
| - Object idValue = null; |
78 |
| - if (idProperty != null) { |
79 |
| - idValue = readFrom(resultSet, idProperty, prefix); |
80 |
| - } |
81 |
| - |
82 |
| - T result = createInstance(entity, resultSet, idValue, prefix); |
83 |
| - |
84 |
| - return entity.requiresPropertyPopulation() // |
85 |
| - ? populateProperties(result, resultSet) // |
86 |
| - : result; |
87 |
| - } |
88 |
| - |
89 |
| - private T populateProperties(T result, ResultSet resultSet) { |
90 |
| - |
91 |
| - PersistentPropertyAccessor<T> propertyAccessor = converter.getPropertyAccessor(entity, result); |
92 |
| - |
93 |
| - Object id = idProperty == null ? null : readFrom(resultSet, idProperty, ""); |
94 |
| - |
95 |
| - PreferredConstructor<T, RelationalPersistentProperty> persistenceConstructor = entity.getPersistenceConstructor(); |
96 |
| - |
97 |
| - for (RelationalPersistentProperty property : entity) { |
98 |
| - |
99 |
| - if (persistenceConstructor != null && persistenceConstructor.isConstructorParameter(property)) { |
100 |
| - continue; |
101 |
| - } |
102 |
| - |
103 |
| - propertyAccessor.setProperty(property, readOrLoadProperty(resultSet, id, property, "")); |
104 |
| - } |
105 |
| - |
106 |
| - return propertyAccessor.getBean(); |
107 |
| - } |
108 |
| - |
109 |
| - @Nullable |
110 |
| - private Object readOrLoadProperty(ResultSet resultSet, @Nullable Object id, RelationalPersistentProperty property, |
111 |
| - String prefix) { |
112 |
| - |
113 |
| - if (property.isCollectionLike() && property.isEntity() && id != null) { |
114 |
| - return accessStrategy.findAllByProperty(id, property); |
115 |
| - } else if (property.isMap() && id != null) { |
116 |
| - return ITERABLE_OF_ENTRY_TO_MAP_CONVERTER.convert(accessStrategy.findAllByProperty(id, property)); |
117 |
| - } else if (property.isEmbedded()) { |
118 |
| - return readEmbeddedEntityFrom(resultSet, id, property, prefix); |
119 |
| - } else { |
120 |
| - return readFrom(resultSet, property, prefix); |
121 |
| - } |
122 |
| - } |
123 |
| - |
124 |
| - /** |
125 |
| - * Read a single value or a complete Entity from the {@link ResultSet} passed as an argument. |
126 |
| - * |
127 |
| - * @param resultSet the {@link ResultSet} to extract the value from. Must not be {@code null}. |
128 |
| - * @param property the {@link RelationalPersistentProperty} for which the value is intended. Must not be {@code null}. |
129 |
| - * @param prefix to be used for all column names accessed by this method. Must not be {@code null}. |
130 |
| - * @return the value read from the {@link ResultSet}. May be {@code null}. |
131 |
| - */ |
132 |
| - @Nullable |
133 |
| - private Object readFrom(ResultSet resultSet, RelationalPersistentProperty property, String prefix) { |
134 |
| - |
135 |
| - if (property.isEntity()) { |
136 |
| - return readEntityFrom(resultSet, property, prefix); |
137 |
| - } |
| 26 | +@Deprecated |
| 27 | +public class EntityRowMapper<T> extends org.springframework.data.jdbc.core.convert.EntityRowMapper<T> { |
138 | 28 |
|
139 |
| - Object value = getObjectFromResultSet(resultSet, prefix + property.getColumnName()); |
140 |
| - return converter.readValue(value, property.getTypeInformation()); |
141 |
| - |
142 |
| - } |
143 |
| - |
144 |
| - private Object readEmbeddedEntityFrom(ResultSet rs, @Nullable Object id, RelationalPersistentProperty property, |
145 |
| - String prefix) { |
146 |
| - |
147 |
| - String newPrefix = prefix + property.getEmbeddedPrefix(); |
148 |
| - |
149 |
| - RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(property.getActualType()); |
150 |
| - |
151 |
| - Object instance = createInstance(entity, rs, null, newPrefix); |
152 |
| - |
153 |
| - @SuppressWarnings("unchecked") |
154 |
| - PersistentPropertyAccessor<?> accessor = converter.getPropertyAccessor((PersistentEntity<Object, ?>) entity, |
155 |
| - instance); |
156 |
| - |
157 |
| - for (RelationalPersistentProperty p : entity) { |
158 |
| - accessor.setProperty(p, readOrLoadProperty(rs, id, p, newPrefix)); |
159 |
| - } |
160 |
| - |
161 |
| - return instance; |
162 |
| - } |
163 |
| - |
164 |
| - @Nullable |
165 |
| - private <S> S readEntityFrom(ResultSet rs, RelationalPersistentProperty property, String prefix) { |
166 |
| - |
167 |
| - String newPrefix = prefix + property.getName() + "_"; |
168 |
| - |
169 |
| - @SuppressWarnings("unchecked") |
170 |
| - RelationalPersistentEntity<S> entity = (RelationalPersistentEntity<S>) context |
171 |
| - .getRequiredPersistentEntity(property.getActualType()); |
172 |
| - |
173 |
| - RelationalPersistentProperty idProperty = entity.getIdProperty(); |
174 |
| - |
175 |
| - Object idValue = null; |
176 |
| - |
177 |
| - if (idProperty != null) { |
178 |
| - idValue = readFrom(rs, idProperty, newPrefix); |
179 |
| - } |
180 |
| - |
181 |
| - if ((idProperty != null // |
182 |
| - ? idValue // |
183 |
| - : getObjectFromResultSet(rs, newPrefix + property.getReverseColumnName()) // |
184 |
| - ) == null) { |
185 |
| - return null; |
186 |
| - } |
187 |
| - |
188 |
| - S instance = createInstance(entity, rs, idValue, newPrefix); |
189 |
| - |
190 |
| - PersistentPropertyAccessor<S> accessor = converter.getPropertyAccessor(entity, instance); |
191 |
| - |
192 |
| - for (RelationalPersistentProperty p : entity) { |
193 |
| - accessor.setProperty(p, readOrLoadProperty(rs, idValue, p, newPrefix)); |
194 |
| - } |
195 |
| - |
196 |
| - return instance; |
| 29 | + public EntityRowMapper(RelationalPersistentEntity<T> entity, JdbcConverter converter, |
| 30 | + DataAccessStrategy accessStrategy) { |
| 31 | + super(entity, converter, accessStrategy); |
197 | 32 | }
|
198 | 33 |
|
199 |
| - @Nullable |
200 |
| - private Object getObjectFromResultSet(ResultSet rs, String backreferenceName) { |
201 |
| - |
202 |
| - try { |
203 |
| - return rs.getObject(backreferenceName); |
204 |
| - } catch (SQLException o_O) { |
205 |
| - throw new MappingException(String.format("Could not read value %s from result set!", backreferenceName), o_O); |
206 |
| - } |
207 |
| - } |
208 |
| - |
209 |
| - private <S> S createInstance(RelationalPersistentEntity<S> entity, ResultSet rs, @Nullable Object idValue, |
210 |
| - String prefix) { |
211 |
| - |
212 |
| - return converter.createInstance(entity, parameter -> { |
213 |
| - |
214 |
| - String parameterName = parameter.getName(); |
215 |
| - |
216 |
| - Assert.notNull(parameterName, "A constructor parameter name must not be null to be used with Spring Data JDBC"); |
217 |
| - |
218 |
| - RelationalPersistentProperty property = entity.getRequiredPersistentProperty(parameterName); |
219 |
| - |
220 |
| - return readOrLoadProperty(rs, idValue, property, prefix); |
221 |
| - }); |
222 |
| - } |
223 | 34 | }
|
0 commit comments