Skip to content

Commit

Permalink
Simplified EntityRowMapper.
Browse files Browse the repository at this point in the history
Removes an unused field and moves operations into  constructor, to make the mapRow method simpler.

Closes #1974
  • Loading branch information
schauder committed Jan 8, 2025
1 parent bb8bde2 commit 8d4b052
Showing 1 changed file with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.domain.RowDocument;
import org.springframework.data.util.TypeInformation;
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
Expand All @@ -38,10 +38,16 @@
*/
public class EntityRowMapper<T> implements RowMapper<T> {

private final RelationalPersistentEntity<T> entity;
private final AggregatePath path;
private final TypeInformation<T> typeInformation;
private final JdbcConverter converter;
private final @Nullable Identifier identifier;
private final Identifier identifier;

private EntityRowMapper(TypeInformation<T> typeInformation, JdbcConverter converter, Identifier identifier) {

this.typeInformation = typeInformation;
this.converter = converter;
this.identifier = identifier;
}

/**
* @deprecated use {@link EntityRowMapper#EntityRowMapper(AggregatePath, JdbcConverter, Identifier)} instead
Expand All @@ -58,29 +64,19 @@ public EntityRowMapper(PersistentPropertyPathExtension path, JdbcConverter conve

@SuppressWarnings("unchecked")
public EntityRowMapper(AggregatePath path, JdbcConverter converter, Identifier identifier) {

this.entity = (RelationalPersistentEntity<T>) path.getLeafEntity();
this.path = path;
this.converter = converter;
this.identifier = identifier;
this(((RelationalPersistentEntity<T>) path.getRequiredLeafEntity()).getTypeInformation(), converter, identifier);
}

public EntityRowMapper(RelationalPersistentEntity<T> entity, JdbcConverter converter) {

this.entity = entity;
this.path = null;
this.converter = converter;
this.identifier = null;
this(entity.getTypeInformation(), converter, Identifier.empty());
}

@Override
public T mapRow(ResultSet resultSet, int rowNumber) throws SQLException {

RowDocument document = RowDocumentResultSetExtractor.toRowDocument(resultSet);

return identifier == null //
? converter.readAndResolve(entity.getTypeInformation(), document, Identifier.empty()) //
: converter.readAndResolve(entity.getTypeInformation(), document, identifier);
return converter.readAndResolve(typeInformation, document, identifier);
}

}

0 comments on commit 8d4b052

Please sign in to comment.