#450 - Add support for @Value when constructing entities using their persistence constructor.

We now support the use of @Value in persistence constructors to compute values for constructor creation.

class MyDomainObject {

	public MyDomainObject(long id, @Value("#root.my_column") String my_column, @Value("5+2") int computed) {
		// …
	}
}
This commit is contained in:
Mark Paluch
2020-09-08 10:30:30 +02:00
parent a1081bbbb2
commit d7a76609b2
5 changed files with 148 additions and 7 deletions

View File

@@ -19,6 +19,9 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import io.r2dbc.spi.Row;
import io.r2dbc.spi.test.MockColumnMetadata;
import io.r2dbc.spi.test.MockRow;
import io.r2dbc.spi.test.MockRowMetadata;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
@@ -31,9 +34,11 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.r2dbc.mapping.OutboundRow;
@@ -208,6 +213,21 @@ public class MappingR2dbcConverterUnitTests {
assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.fromOrEmpty(1L, Long.TYPE));
}
@Test // gh-59
public void shouldEvaluateSpelExpression() {
MockRow row = MockRow.builder().identified("id", Object.class, 42).identified("world", Object.class, "No, universe")
.build();
MockRowMetadata metadata = MockRowMetadata.builder().columnMetadata(MockColumnMetadata.builder().name("id").build())
.columnMetadata(MockColumnMetadata.builder().name("world").build()).build();
WithSpelExpression result = converter.read(WithSpelExpression.class, row, metadata);
assertThat(result.id).isEqualTo(42);
assertThat(result.hello).isNull();
assertThat(result.world).isEqualTo("No, universe");
}
@AllArgsConstructor
static class Person {
@Id String id;
@@ -312,4 +332,17 @@ public class MappingR2dbcConverterUnitTests {
return person;
}
}
static class WithSpelExpression {
private long id;
@Transient String hello;
@Transient String world;
public WithSpelExpression(long id, @Value("null") String hello, @Value("#root.world") String world) {
this.id = id;
this.hello = hello;
this.world = world;
}
}
}