Skip to content

Commit

Permalink
jakarta.annotation.Nonnull implies optional=false
Browse files Browse the repository at this point in the history
see issue jakartaee#614

Signed-off-by: Gavin King <[email protected]>
  • Loading branch information
gavinking committed Sep 20, 2024
1 parent 6f0e015 commit 36c435a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
8 changes: 6 additions & 2 deletions api/src/main/java/jakarta/persistence/Basic.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@
* (Optional) Specifies whether the value of the field or
* property may be null.
*
* <p>This is a hint and is disregarded for primitive types;
* it may be used in schema generation to infer that the
* <p>If the annotated field or property is of primitive type,
* or if it is also annotated {@code @jakarta.annotation.Nonnull},
* then {@code optional=false} is implied, and the value of this
* annotation member is ignored.
*
* <p>May be used in schema generation to infer that the
* mapped column is {@link Column#nullable not null}.
*
* <p>If not specified, defaults to {@code true}.
Expand Down
8 changes: 6 additions & 2 deletions api/src/main/java/jakarta/persistence/ManyToOne.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@
FetchType fetch() default FetchType.EAGER;

/**
* (Optional) Whether the association is optional. If set
* to false then a non-null relationship must always exist.
* (Optional) Whether the association is optional. If set to
* {@code false} then a non-null relationship must always exist.
*
* <p>If the annotated field or property is also annotated
* {@code @jakarta.annotation.Nonnull}, then {@code optional=false}
* is implied, and the value of this annotation member is ignored.
*
* <p>May be used in schema generation to infer that the
* mapped foreign key column is {@link JoinColumn#nullable
Expand Down
10 changes: 8 additions & 2 deletions api/src/main/java/jakarta/persistence/OneToOne.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,14 @@
FetchType fetch() default FetchType.EAGER;

/**
* (Optional) Whether the association is optional. If set
* to false then a non-null relationship must always exist.
* (Optional) Whether the association is optional. If set to
* {@code false} then a non-null relationship must always exist.
*
* <p>If the annotated field or property is also annotated
* {@code @jakarta.annotation.Nonnull}, then {@code optional=false}
* is implied, and the value of this annotation member is ignored.
*
*
*/
boolean optional() default true;

Expand Down
34 changes: 22 additions & 12 deletions spec/src/main/asciidoc/ch11-metadata-for-or-mapping.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,9 @@ strategy hint has been specified. In particular, lazy fetching might
only be available for `Basic` mappings for which property-based access
is used.

The `optional` element is a hint as to
whether the value of the field or property may be null. It is
disregarded for primitive types.
If the annotated field or property is of primitive type, or if it is
also annotated with `jakarta.annotation.Nonnull`, then `optional=false`
is implied, and the value of the `optional` annotation member is ignored.

.Basic Annotation Elements
[#a14218,options="header"]
Expand All @@ -590,9 +590,9 @@ strategy is a requirement on the persistence provider runtime that the
value must be eagerly fetched. The LAZY strategy is a hint to the
persistence provider runtime. |EAGER
|boolean |optional
|(Optional) Whether the value of the field or
property may be null. This is a hint and is disregarded for primitive
types; it may be used in schema generation.
|(Optional) Whether the value of the field or property may be null.
May be used in schema generation. Ignored if the field or property
is of primitive type or is annotated `Nonnull`.
|true
|===

Expand Down Expand Up @@ -2983,6 +2983,10 @@ runtime that the associated entity should be fetched lazily when it is
first accessed. The implementation is permitted to eagerly fetch
associations for which the `LAZY` strategy hint has been specified.

If the annotated field or property is also annotated with
`jakarta.annotation.Nonnull`, then `optional=false` is implied
and the value of the `optional` annotation member is ignored.

.ManyToOne Annotation Elements
[#a15202,options="header"]
|===
Expand Down Expand Up @@ -3010,9 +3014,10 @@ persistence provider runtime.

|boolean
|optional
|(Optional) Whether the association is
optional. If set to false then a non-null relationship must always
exist.
|(Optional) Whether the association is optional. If set to false,
then a non-null relationship must always exist.
May be used in schema generation. Ignored if the field or property
is annotated `Nonnull`.
|true
|===

Expand Down Expand Up @@ -4118,6 +4123,10 @@ applications must otherwise not depend upon a specific order of removal,
and must not reassign an entity that has been orphaned to another
relationship or otherwise attempt to persist it.

If the annotated field or property is also annotated with
`jakarta.annotation.Nonnull`, then `optional=false` is implied
and the value of the `optional` annotation member is ignored.

.OneToOne Annotation Elements
[#a15735,options="header"]
|===
Expand All @@ -4144,9 +4153,10 @@ persistence provider runtime.

|boolean
|optional
|(Optional) Whether the association is
optional. If set to false then a non-null relationship must always
exist.
|(Optional) Whether the association is optional. If set to false,
then a non-null relationship must always exist.
May be used in schema generation. Ignored if the field or property
is annotated `Nonnull`.
|true

|String
Expand Down

0 comments on commit 36c435a

Please sign in to comment.