Browse Source

Simplified EntityRowMapper.

Removes an unused field and moves operations into  constructor, to make the mapRow method simpler.

Closes #1974
pull/1969/head
Jens Schauder 11 months ago
parent
commit
dfd8123a07
No known key found for this signature in database
GPG Key ID: 74F6C554AE971567
  1. 28
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/EntityRowMapper.java

28
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/EntityRowMapper.java

@ -21,8 +21,8 @@ import java.sql.SQLException;
import org.springframework.data.relational.core.mapping.AggregatePath; import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.domain.RowDocument; import org.springframework.data.relational.domain.RowDocument;
import org.springframework.data.util.TypeInformation;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
/** /**
* Maps a {@link ResultSet} to an entity of type {@code T}, including entities referenced. This {@link RowMapper} might * Maps a {@link ResultSet} to an entity of type {@code T}, including entities referenced. This {@link RowMapper} might
@ -37,26 +37,24 @@ import org.springframework.lang.Nullable;
*/ */
public class EntityRowMapper<T> implements RowMapper<T> { public class EntityRowMapper<T> implements RowMapper<T> {
private final RelationalPersistentEntity<T> entity; private final TypeInformation<T> typeInformation;
private final AggregatePath path;
private final JdbcConverter converter; private final JdbcConverter converter;
private final @Nullable Identifier identifier; private final Identifier identifier;
@SuppressWarnings("unchecked") private EntityRowMapper(TypeInformation<T> typeInformation, JdbcConverter converter, Identifier identifier) {
public EntityRowMapper(AggregatePath path, JdbcConverter converter, Identifier identifier) {
this.entity = (RelationalPersistentEntity<T>) path.getLeafEntity(); this.typeInformation = typeInformation;
this.path = path;
this.converter = converter; this.converter = converter;
this.identifier = identifier; this.identifier = identifier;
} }
public EntityRowMapper(RelationalPersistentEntity<T> entity, JdbcConverter converter) { @SuppressWarnings("unchecked")
public EntityRowMapper(AggregatePath path, JdbcConverter converter, Identifier identifier) {
this(((RelationalPersistentEntity<T>) path.getRequiredLeafEntity()).getTypeInformation(), converter, identifier);
}
this.entity = entity; public EntityRowMapper(RelationalPersistentEntity<T> entity, JdbcConverter converter) {
this.path = null; this(entity.getTypeInformation(), converter, Identifier.empty());
this.converter = converter;
this.identifier = null;
} }
@Override @Override
@ -64,9 +62,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
RowDocument document = RowDocumentResultSetExtractor.toRowDocument(resultSet); RowDocument document = RowDocumentResultSetExtractor.toRowDocument(resultSet);
return identifier == null // return converter.readAndResolve(typeInformation, document, identifier);
? converter.readAndResolve(entity.getTypeInformation(), document, Identifier.empty()) //
: converter.readAndResolve(entity.getTypeInformation(), document, identifier);
} }
} }

Loading…
Cancel
Save