DATAJDBC-508 - Add support for @Value in persistence constructors.

We now evaluate @Value annotations in persistence constructors to compute values when creating object instances. AtValue can be used to materialize values for e.g. transient properties. Root properties map to the ResultSet from which an object gets materialized.

class WithAtValue {

	private final @Id Long id;
	private final @Transient String computed;

	public WithAtValue(Long id,
			@Value("#root.first_name") String computed) { // obtain value from first_name column
		this.id = id;
			this.computed = computed;
		}
	}
This commit is contained in:
Mark Paluch
2020-09-22 11:48:56 +02:00
parent 842a309f27
commit 9cd8fc16b3
4 changed files with 198 additions and 12 deletions

View File

@@ -51,6 +51,7 @@ import org.mockito.stubbing.Answer;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.Transient;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.mapping.PersistentPropertyPath;
@@ -642,6 +643,19 @@ public class EntityRowMapperUnitTests {
assertThat(result.child).isNull();
}
@Test // DATAJDBC-508
public void materializesObjectWithAtValue() throws SQLException {
ResultSet rs = mockResultSet(asList("ID", "FIRST_NAME"), //
123L, "Hello World");
rs.next();
WithAtValue result = createRowMapper(WithAtValue.class).mapRow(rs, 1);
assertThat(result.getId()).isEqualTo(123L);
assertThat(result.getComputed()).isEqualTo("Hello World");
}
// Model classes to be used in tests
@With
@@ -1221,4 +1235,17 @@ public class EntityRowMapperUnitTests {
final Object expectedValue;
final String sourceColumn;
}
@Getter
private static class WithAtValue {
@Id private final Long id;
private final @Transient String computed;
public WithAtValue(Long id,
@org.springframework.beans.factory.annotation.Value("#root.first_name") String computed) {
this.id = id;
this.computed = computed;
}
}
}