Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
* @author Jens Schauder
* @author Christoph Strobl
* @author Myeonghyeon Lee
* @since 1.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure to use a formatter so that formatting and ordering of annotation/imports does not get changed.

* @see MappingContext
* @see SimpleTypeHolder
* @see CustomConversions
* @since 1.1
*/
public class BasicJdbcConverter extends BasicRelationalConverter implements JdbcConverter {

Expand Down Expand Up @@ -324,7 +324,35 @@ private T populateProperties(T instance, @Nullable Object idValue) {
continue;
}

propertyAccessor.setProperty(property, readOrLoadProperty(idValue, property));
// check if property is in the result set
// if not - leave it out
// DATAJDBC-341
if (property.isEntity() || property.isEmbedded()) {
propertyAccessor.setProperty(property, readOrLoadProperty(idValue, property));
} else {
try {
if (resultSet.findColumn(property.getColumnName()) > 0) {
propertyAccessor.setProperty(property, readOrLoadProperty(idValue, property));
} else {
try {
propertyAccessor.setProperty(property, readOrLoadProperty(idValue, property));
} catch (Exception exception) {
LOG.info(
"The result set is not corresponding to the target entity. Left out properties will be set to standard values (NULL for reference types, 0 for primitives.");
}
}
} catch (SQLException e) {
String columnAlias = path.extendBy(property).getColumnAlias();
try {
if (resultSet.findColumn(columnAlias) > 0) {
propertyAccessor.setProperty(property, readOrLoadProperty(idValue, property));
}
} catch (SQLException ex) {
LOG.info(String.format("Cannot find column named %s within the result set!", property.getColumnName()));
}
}
}

}

return propertyAccessor.getBean();
Expand Down Expand Up @@ -444,7 +472,8 @@ private Object getObjectFromResultSet(String backreferenceName) {
try {
return resultSet.getObject(backreferenceName);
} catch (SQLException o_O) {
throw new MappingException(String.format("Could not read value %s from result set!", backreferenceName), o_O);
LOG.info(String.format("Could not read value %s from result set! Use null as value.", backreferenceName), o_O);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -85,6 +81,35 @@ public String getColumnName(RelationalPersistentProperty property) {
}
};

@Test // DATAJDBC-341
public void mapNotNeededValueTypePropertiesToNull() throws SQLException {
ResultSet rs = mockResultSet(singletonList("id"), //
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha");
rs.next();
Trivial extracted = createRowMapper(Trivial.class).mapRow(rs, 1);

assertThat(extracted) //
.isNotNull() //
.extracting(e -> e.id, e -> e.name) //
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, null);

}

@Test // DATAJDBC-341
public void mapNotNeededPrimitiveTypePropertiesToNull() throws SQLException {
ResultSet rs = mockResultSet(singletonList("id"), //
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "alpha");
rs.next();
TrivialMapPropertiesToNullIfNotNeeded extracted = createRowMapper(TrivialMapPropertiesToNullIfNotNeeded.class)
.mapRow(rs, 1);

assertThat(extracted) //
.isNotNull() //
.extracting(e -> e.id, e -> e.age) //
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, 0);

}

@Test // DATAJDBC-113
public void simpleEntitiesGetProperlyExtracted() throws SQLException {

Expand Down Expand Up @@ -475,6 +500,19 @@ static class Trivial {
String name;
}

@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Getter
static class TrivialMapPropertiesToNullIfNotNeeded {

@Id Long id;
int age;
String phone;
Boolean isSupreme;
long referenceToCustomer;
}

@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
Expand Down Expand Up @@ -760,11 +798,18 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
return isAfterLast() || isBeforeFirst() ? 0 : index + 1;
case "toString":
return this.toString();
case "findColumn":
return isThereAColumnNamed(invocation.getArgument(0));
default:
throw new OperationNotSupportedException(invocation.getMethod().getName());
}
}

private int isThereAColumnNamed(String name) {
Optional<Map<String, Object>> first = values.stream().filter(s -> s.equals(name)).findFirst();
return (first.isPresent()) ? 1 : 0;
}

private boolean isAfterLast() {
return index >= values.size() && !values.isEmpty();
}
Expand Down