DATAJDBC-374 - Add onEmpty attribute to Embedded annotation.
The onEmpty attribute allows to define if an embedded entity should be set to null or a default instance if all properties backing the entity are actually null.
@Embedded(onEmpty = USE_NULL)
EmbeddedEntity embeddedEntity;
Original pull request: #154.
pull/156/head
Christoph Strobl7 years agocommitted byJens Schauder
@ -288,14 +288,19 @@ Embedded entities are used to have value objects in your java data model, even i
In the following example you see, that `MyEntity` is mapped with the `@Embedded` annotation.
In the following example you see, that `MyEntity` is mapped with the `@Embedded` annotation.
The consequence of this is, that in the database a table `my_entity` with the two columns `id` and `name` (from the `EmbeddedEntity` class) is expected.
The consequence of this is, that in the database a table `my_entity` with the two columns `id` and `name` (from the `EmbeddedEntity` class) is expected.
However, if the `name` column is actually `null` within the result set, the entire property `embeddedEntity` will be set to null according to the `onEmpty` of `@Embedded`, which ``null``s objects when all nested properties are `null`. +
Opposite to this behavior `USE_EMPTY` tries to create a new instance using either a default constructor or one that accepts nullable parameter values from the result set.
.Sample Code of embedding objects
====
====
[source, java]
[source, java]
----
----
public class MyEntity {
public class MyEntity {
@Id
@Id
Integer id;
Integer id;
@Embedded
@Embedded(onEmpty = USE_NULL) <1>
EmbeddedEntity embeddedEntity;
EmbeddedEntity embeddedEntity;
}
}
@ -303,9 +308,10 @@ public class EmbeddedEntity {
String name;
String name;
}
}
----
----
<1> ``Null``s `embeddedEntity` if `name` in `null`. Use `USE_EMPTY` to instanciate `embeddedEntity` with a potential `null` value for the `name` property.
====
====
If you need a value object multiple times in an entity, this can be achieved with the optional `value` element of the `@Embedded` annotation.
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.
This element represents a prefix and is prepend for each column name in the embedded object.