Skip to content

Commit 55a3f9c

Browse files
christophstroblschauder
authored andcommitted
DATAJDBC-374 - Introduce shortcuts for Embedded#onEmpty(…)
@Embedded.Nullable & @Embedded.Empty offer shortcuts for @Embedded(onEmpty = USE_NULL) and @Embedded(onEmpty = USE_EMPTY) to reduce verbositility and simultaneously set JSR-305 @javax.annotation.Nonnull accordingly. @Embedded.Nullable EmbeddedEntity embeddedEntity; Original pull request: #154.
1 parent 5830b83 commit 55a3f9c

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<postgresql.version>42.0.0</postgresql.version>
3535
<mariadb-java-client.version>2.2.3</mariadb-java-client.version>
3636
<testcontainers.version>1.9.1</testcontainers.version>
37+
<jsr305.version>3.0.2</jsr305.version>
3738
</properties>
3839

3940
<inceptionYear>2017</inceptionYear>

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/EntityRowMapperUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,13 @@ static class WithNullableEmbeddedImmutableValue {
558558
static class WithEmptyEmbeddedImmutableValue {
559559

560560
@Id Long id;
561-
@Embedded(onEmpty = OnEmpty.USE_EMPTY) ImmutableValue embeddedImmutableValue;
561+
@Embedded.Empty ImmutableValue embeddedImmutableValue;
562562
}
563563

564564
static class WithEmbeddedPrimitiveImmutableValue {
565565

566566
@Id Long id;
567-
@Embedded(onEmpty = OnEmpty.USE_NULL) ImmutablePrimitiveValue embeddedImmutablePrimitiveValue;
567+
@Embedded.Nullable ImmutablePrimitiveValue embeddedImmutablePrimitiveValue;
568568
}
569569

570570
@Value

spring-data-relational/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
<artifactId>spring-core</artifactId>
4949
</dependency>
5050

51+
<dependency>
52+
<groupId>com.google.code.findbugs</groupId>
53+
<artifactId>jsr305</artifactId>
54+
<version>${jsr305.version}</version>
55+
<optional>true</optional>
56+
</dependency>
57+
5158
<dependency>
5259
<groupId>org.assertj</groupId>
5360
<artifactId>assertj-core</artifactId>

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/Embedded.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import javax.annotation.meta.When;
25+
26+
import org.springframework.core.annotation.AliasFor;
27+
2428
/**
2529
* The annotation to configure a value object as embedded in the current table.
2630
* <p />
@@ -38,6 +42,8 @@
3842

3943
/**
4044
* Set the load strategy for the embedded object if all contained fields yield {@literal null} values.
45+
* <p />
46+
* {@link Nullable @Embedded.Nullable} and {@link Empty @Embedded.Empty} offer shortcuts for this.
4147
*
4248
* @return never {@link} null.
4349
*/
@@ -57,4 +63,84 @@
5763
enum OnEmpty {
5864
USE_NULL, USE_EMPTY
5965
}
66+
67+
/**
68+
* Shortcut for a nullable embedded property.
69+
*
70+
* <pre>
71+
* <code>
72+
* &#64;Embedded.Nullable
73+
* private Address address;
74+
* </code>
75+
* </pre>
76+
*
77+
* as alternative to the more verbose
78+
*
79+
* <pre>
80+
* <code>
81+
*
82+
* &#64;Embedded(onEmpty = USE_NULL)
83+
* &#64;javax.annotation.Nonnull(when = When.MAYBE)
84+
* private Address address;
85+
*
86+
* </code>
87+
* </pre>
88+
*
89+
* @author Christoph Strobl
90+
* @since 1.1
91+
* @see Embedded#onEmpty()
92+
*/
93+
@Embedded(onEmpty = OnEmpty.USE_NULL)
94+
@Documented
95+
@Retention(RetentionPolicy.RUNTIME)
96+
@Target({ ElementType.FIELD, ElementType.METHOD })
97+
@javax.annotation.Nonnull(when = When.MAYBE)
98+
@interface Nullable {
99+
100+
/**
101+
* @return prefix for columns in the embedded value object. An empty {@link String} by default.
102+
*/
103+
@AliasFor(annotation = Embedded.class, attribute = "prefix")
104+
String prefix() default "";
105+
}
106+
107+
/**
108+
* Shortcut for an empty embedded property.
109+
*
110+
* <pre>
111+
* <code>
112+
* &#64;Embedded.Empty
113+
* private Address address;
114+
* </code>
115+
* </pre>
116+
*
117+
* as alternative to the more verbose
118+
*
119+
* <pre>
120+
* <code>
121+
*
122+
* &#64;Embedded(onEmpty = USE_EMPTY)
123+
* &#64;javax.annotation.Nonnull(when = When.NEVER)
124+
* private Address address;
125+
*
126+
* </code>
127+
* </pre>
128+
*
129+
* @author Christoph Strobl
130+
* @since 1.1
131+
* @see Embedded#onEmpty()
132+
*/
133+
@Embedded(onEmpty = OnEmpty.USE_EMPTY)
134+
@Documented
135+
@Retention(RetentionPolicy.RUNTIME)
136+
@Target({ ElementType.FIELD, ElementType.METHOD })
137+
@javax.annotation.Nonnull(when = When.NEVER)
138+
@interface Empty {
139+
140+
/**
141+
* @return prefix for columns in the embedded value object. An empty {@link String} by default.
142+
*/
143+
@AliasFor(annotation = Embedded.class, attribute = "prefix")
144+
String prefix() default "";
145+
}
60146
}

src/main/asciidoc/jdbc.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ public class EmbeddedEntity {
314314
If you need a value object multiple times in an entity, this can be achieved with the optional `prefix` element of the `@Embedded` annotation.
315315
This element represents a prefix and is prepend for each column name in the embedded object.
316316

317+
[TIP]
318+
====
319+
Make use of the shortcuts `@Embedded.Nullable` & `@Embedded.Empty` for `@Embedded(onEmpty = USE_NULL)` and `@Embedded(onEmpty = USE_EMPTY)` to reduce verbositility and simultaneously set JSR-305 `@javax.annotation.Nonnull` accordingly.
320+
321+
[source, java]
322+
----
323+
public class MyEntity {
324+
325+
@Id
326+
Integer id;
327+
328+
@Embedded.Nullable <1>
329+
EmbeddedEntity embeddedEntity;
330+
}
331+
----
332+
<1> Shortcut for `@Embedded(onEmpty = USE_NULL)`.
333+
====
334+
317335
[[jdbc.entity-persistence.state-detection-strategies]]
318336
=== Entity State Detection Strategies
319337

0 commit comments

Comments
 (0)