org.assertj
assertj-core
diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/Embedded.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/Embedded.java
index 6c0afc7c3..4e83767a5 100644
--- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/Embedded.java
+++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/Embedded.java
@@ -21,6 +21,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+import javax.annotation.meta.When;
+
+import org.springframework.core.annotation.AliasFor;
+
/**
* The annotation to configure a value object as embedded in the current table.
*
@@ -38,6 +42,8 @@ public @interface Embedded {
/**
* Set the load strategy for the embedded object if all contained fields yield {@literal null} values.
+ *
+ * {@link Nullable @Embedded.Nullable} and {@link Empty @Embedded.Empty} offer shortcuts for this.
*
* @return never {@link} null.
*/
@@ -57,4 +63,84 @@ public @interface Embedded {
enum OnEmpty {
USE_NULL, USE_EMPTY
}
+
+ /**
+ * Shortcut for a nullable embedded property.
+ *
+ *
+ *
+ * @Embedded.Nullable
+ * private Address address;
+ *
+ *
+ *
+ * as alternative to the more verbose
+ *
+ *
+ *
+ *
+ * @Embedded(onEmpty = USE_NULL)
+ * @javax.annotation.Nonnull(when = When.MAYBE)
+ * private Address address;
+ *
+ *
+ *
+ *
+ * @author Christoph Strobl
+ * @since 1.1
+ * @see Embedded#onEmpty()
+ */
+ @Embedded(onEmpty = OnEmpty.USE_NULL)
+ @Documented
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ ElementType.FIELD, ElementType.METHOD })
+ @javax.annotation.Nonnull(when = When.MAYBE)
+ @interface Nullable {
+
+ /**
+ * @return prefix for columns in the embedded value object. An empty {@link String} by default.
+ */
+ @AliasFor(annotation = Embedded.class, attribute = "prefix")
+ String prefix() default "";
+ }
+
+ /**
+ * Shortcut for an empty embedded property.
+ *
+ *
+ *
+ * @Embedded.Empty
+ * private Address address;
+ *
+ *
+ *
+ * as alternative to the more verbose
+ *
+ *
+ *
+ *
+ * @Embedded(onEmpty = USE_EMPTY)
+ * @javax.annotation.Nonnull(when = When.NEVER)
+ * private Address address;
+ *
+ *
+ *
+ *
+ * @author Christoph Strobl
+ * @since 1.1
+ * @see Embedded#onEmpty()
+ */
+ @Embedded(onEmpty = OnEmpty.USE_EMPTY)
+ @Documented
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ ElementType.FIELD, ElementType.METHOD })
+ @javax.annotation.Nonnull(when = When.NEVER)
+ @interface Empty {
+
+ /**
+ * @return prefix for columns in the embedded value object. An empty {@link String} by default.
+ */
+ @AliasFor(annotation = Embedded.class, attribute = "prefix")
+ String prefix() default "";
+ }
}
diff --git a/src/main/asciidoc/jdbc.adoc b/src/main/asciidoc/jdbc.adoc
index b6bc0fd33..77d976108 100644
--- a/src/main/asciidoc/jdbc.adoc
+++ b/src/main/asciidoc/jdbc.adoc
@@ -314,6 +314,24 @@ public class EmbeddedEntity {
If you need a value object multiple times in an entity, this can be achieved with the optional `prefix` element of the `@Embedded` annotation.
This element represents a prefix and is prepend for each column name in the embedded object.
+[TIP]
+====
+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.
+
+[source, java]
+----
+public class MyEntity {
+
+ @Id
+ Integer id;
+
+ @Embedded.Nullable <1>
+ EmbeddedEntity embeddedEntity;
+}
+----
+<1> Shortcut for `@Embedded(onEmpty = USE_NULL)`.
+====
+
[[jdbc.entity-persistence.state-detection-strategies]]
=== Entity State Detection Strategies