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
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-374-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down Expand Up @@ -34,6 +34,7 @@
<postgresql.version>42.0.0</postgresql.version>
<mariadb-java-client.version>2.2.3</mariadb-java-client.version>
<testcontainers.version>1.9.1</testcontainers.version>
<jsr305.version>3.0.2</jsr305.version>
</properties>

<inceptionYear>2017</inceptionYear>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-374-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-374-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-374-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.relational.core.conversion.BasicRelationalConverter;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
Expand Down Expand Up @@ -352,6 +354,11 @@ private Object readFrom(RelationalPersistentProperty property) {
private Object readEmbeddedEntityFrom(@Nullable Object idValue, RelationalPersistentProperty property) {

ReadingContext<?> newContext = extendBy(property);

if(OnEmpty.USE_EMPTY.equals(property.findAnnotation(Embedded.class).onEmpty())) {
return newContext.createInstanceInternal(idValue);
}

return newContext.hasInstanceValues(idValue) ? newContext.createInstanceInternal(idValue) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
Expand Down Expand Up @@ -217,15 +218,15 @@ PersistentPropertyPath<RelationalPersistentProperty> createSimplePath(String pat
static class DummyEntity {
@Id Long entityId;
Second second;
@Embedded("sec") Second second2;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "sec") Second second2;
List<Second> secondList;
WithId withId;
}

@SuppressWarnings("unused")
static class Second {
Third third;
@Embedded("thrd") Third third2;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "thrd") Third third2;
}

@SuppressWarnings("unused")
Expand All @@ -237,7 +238,7 @@ static class Third {
static class WithId {
@Id Long withIdId;
Second second;
@Embedded("sec") Second second2;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "sec") Second second2;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
Expand Down Expand Up @@ -291,20 +292,35 @@ public void chainedEntitiesWithoutId() throws SQLException {
}

@Test // DATAJDBC-370
public void simpleImmutableEmbeddedGetsProperlyExtracted() throws SQLException {
public void simpleNullableImmutableEmbeddedGetsProperlyExtracted() throws SQLException {

ResultSet rs = mockResultSet(asList("id", "value"), //
ID_FOR_ENTITY_NOT_REFERENCING_MAP, "ru'Ha'");
rs.next();

WithImmutableValue extracted = createRowMapper(WithImmutableValue.class).mapRow(rs, 1);
WithNullableEmbeddedImmutableValue extracted = createRowMapper(WithNullableEmbeddedImmutableValue.class).mapRow(rs, 1);

assertThat(extracted) //
.isNotNull() //
.extracting(e -> e.id, e -> e.embeddedImmutableValue) //
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, new ImmutableValue("ru'Ha'"));
}

@Test // DATAJDBC-374
public void simpleEmptyImmutableEmbeddedGetsProperlyExtracted() throws SQLException {

ResultSet rs = mockResultSet(asList("id", "value"), //
ID_FOR_ENTITY_NOT_REFERENCING_MAP, null);
rs.next();

WithEmptyEmbeddedImmutableValue extracted = createRowMapper(WithEmptyEmbeddedImmutableValue.class).mapRow(rs, 1);

assertThat(extracted) //
.isNotNull() //
.extracting(e -> e.id, e -> e.embeddedImmutableValue) //
.containsExactly(ID_FOR_ENTITY_NOT_REFERENCING_MAP, new ImmutableValue(null));
}

@Test // DATAJDBC-370
@SneakyThrows
public void simplePrimitiveImmutableEmbeddedGetsProperlyExtracted() {
Expand All @@ -313,7 +329,7 @@ public void simplePrimitiveImmutableEmbeddedGetsProperlyExtracted() {
ID_FOR_ENTITY_NOT_REFERENCING_MAP, 24);
rs.next();

WithPrimitiveImmutableValue extracted = createRowMapper(WithPrimitiveImmutableValue.class).mapRow(rs, 1);
WithEmbeddedPrimitiveImmutableValue extracted = createRowMapper(WithEmbeddedPrimitiveImmutableValue.class).mapRow(rs, 1);

assertThat(extracted) //
.isNotNull() //
Expand All @@ -328,7 +344,7 @@ public void simpleImmutableEmbeddedShouldBeNullIfAllOfTheEmbeddableAreNull() thr
ID_FOR_ENTITY_NOT_REFERENCING_MAP, null);
rs.next();

WithImmutableValue extracted = createRowMapper(WithImmutableValue.class).mapRow(rs, 1);
WithNullableEmbeddedImmutableValue extracted = createRowMapper(WithNullableEmbeddedImmutableValue.class).mapRow(rs, 1);

assertThat(extracted) //
.isNotNull() //
Expand Down Expand Up @@ -376,7 +392,7 @@ public void primitiveEmbeddedShouldBeNullWhenNoValuePresent() {
ID_FOR_ENTITY_NOT_REFERENCING_MAP, null);
rs.next();

WithPrimitiveImmutableValue extracted = createRowMapper(WithPrimitiveImmutableValue.class).mapRow(rs, 1);
WithEmbeddedPrimitiveImmutableValue extracted = createRowMapper(WithEmbeddedPrimitiveImmutableValue.class).mapRow(rs, 1);

assertThat(extracted) //
.isNotNull() //
Expand Down Expand Up @@ -460,7 +476,7 @@ static class EmbeddedEntity {

@Id Long id;
String name;
@Embedded("prefix_") Trivial children;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_") Trivial children;
}

private static class DontUseSetter {
Expand Down Expand Up @@ -528,16 +544,22 @@ static class NoIdChain4 {
NoIdChain3 chain3;
}

static class WithImmutableValue {
static class WithNullableEmbeddedImmutableValue {

@Id Long id;
@Embedded(onEmpty = OnEmpty.USE_NULL) ImmutableValue embeddedImmutableValue;
}

static class WithEmptyEmbeddedImmutableValue {

@Id Long id;
@Embedded ImmutableValue embeddedImmutableValue;
@Embedded.Empty ImmutableValue embeddedImmutableValue;
}

static class WithPrimitiveImmutableValue {
static class WithEmbeddedPrimitiveImmutableValue {

@Id Long id;
@Embedded ImmutablePrimitiveValue embeddedImmutablePrimitiveValue;
@Embedded.Nullable ImmutablePrimitiveValue embeddedImmutablePrimitiveValue;
}

@Value
Expand All @@ -554,13 +576,13 @@ static class WithDeepNestedEmbeddable {

@Id Long id;
String level0;
@Embedded("level1_") EmbeddedWithEmbedded level1;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "level1_") EmbeddedWithEmbedded level1;
}

static class EmbeddedWithEmbedded {

Object value;
@Embedded("level2_") ImmutableValue level2;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "level2_") ImmutableValue level2;
}

// Infrastructure for assertions and constructing mocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
Expand Down Expand Up @@ -245,16 +246,16 @@ static class DummyEntity {

@Column("id1") @Id Long id;

@Embedded("prefix_") CascadedEmbedded prefixedEmbeddable;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_") CascadedEmbedded prefixedEmbeddable;

@Embedded CascadedEmbedded embeddable;
@Embedded(onEmpty = OnEmpty.USE_NULL) CascadedEmbedded embeddable;
}

@SuppressWarnings("unused")
static class CascadedEmbedded {
String test;
@Embedded("prefix2_") Embeddable prefixedEmbeddable;
@Embedded Embeddable embeddable;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix2_") Embeddable prefixedEmbeddable;
@Embedded(onEmpty = OnEmpty.USE_NULL) Embeddable embeddable;
}

@SuppressWarnings("unused")
Expand All @@ -268,7 +269,7 @@ static class DummyEntity2 {

@Id Long id;

@Embedded("prefix_") EmbeddedWithReference embedded;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_") EmbeddedWithReference embedded;
}

static class EmbeddedWithReference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
Expand Down Expand Up @@ -99,7 +100,7 @@ static class DummyEntity {

@Id Long id;

@Embedded("prefix_") Embeddable prefixedEmbeddable;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_") Embeddable prefixedEmbeddable;
}

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
Expand Down Expand Up @@ -250,16 +251,16 @@ static class DummyEntity {

@Id Long id;

@Embedded("prefix_") CascadedEmbeddable prefixedEmbeddable;
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_") CascadedEmbeddable prefixedEmbeddable;

@Embedded CascadedEmbeddable embeddable;
@Embedded(onEmpty = OnEmpty.USE_NULL) CascadedEmbeddable embeddable;
}

@Data
static class CascadedEmbeddable {
String test;

@Embedded("prefix2_")
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix2_")
Embeddable embeddable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
Expand Down Expand Up @@ -246,7 +247,7 @@ static class DummyEntity2 {

String test;

@Embedded("prefix_")
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_")
Embeddable embeddable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -249,7 +250,7 @@ private static class DummyEntity {

String test;

@Embedded("prefix_")
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_")
Embeddable embeddable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Embedded.OnEmpty;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
Expand Down Expand Up @@ -236,7 +237,7 @@ private static class DummyEntity {

String test;

@Embedded("prefix_")
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_")
Embeddable embeddable;
}

Expand Down
11 changes: 9 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-374-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-374-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -48,6 +48,13 @@
<artifactId>spring-core</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>${jsr305.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
this.isEmbedded = Lazy.of(() -> Optional.ofNullable(findAnnotation(Embedded.class)).isPresent());

this.embeddedPrefix = Lazy.of(() -> Optional.ofNullable(findAnnotation(Embedded.class)) //
.map(Embedded::value) //
.map(Embedded::prefix) //
.orElse(""));

this.columnName = Lazy.of(() -> Optional.ofNullable(findAnnotation(Column.class)) //
Expand Down
Loading