Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 36c435a

Browse files
committedSep 20, 2024·
jakarta.annotation.Nonnull implies optional=false
see issue #614 Signed-off-by: Gavin King <[email protected]>
1 parent 6f0e015 commit 36c435a

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed
 

‎api/src/main/java/jakarta/persistence/Basic.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@
9797
* (Optional) Specifies whether the value of the field or
9898
* property may be null.
9999
*
100-
* <p>This is a hint and is disregarded for primitive types;
101-
* it may be used in schema generation to infer that the
100+
* <p>If the annotated field or property is of primitive type,
101+
* or if it is also annotated {@code @jakarta.annotation.Nonnull},
102+
* then {@code optional=false} is implied, and the value of this
103+
* annotation member is ignored.
104+
*
105+
* <p>May be used in schema generation to infer that the
102106
* mapped column is {@link Column#nullable not null}.
103107
*
104108
* <p>If not specified, defaults to {@code true}.

‎api/src/main/java/jakarta/persistence/ManyToOne.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@
123123
FetchType fetch() default FetchType.EAGER;
124124

125125
/**
126-
* (Optional) Whether the association is optional. If set
127-
* to false then a non-null relationship must always exist.
126+
* (Optional) Whether the association is optional. If set to
127+
* {@code false} then a non-null relationship must always exist.
128+
*
129+
* <p>If the annotated field or property is also annotated
130+
* {@code @jakarta.annotation.Nonnull}, then {@code optional=false}
131+
* is implied, and the value of this annotation member is ignored.
128132
*
129133
* <p>May be used in schema generation to infer that the
130134
* mapped foreign key column is {@link JoinColumn#nullable

‎api/src/main/java/jakarta/persistence/OneToOne.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@
163163
FetchType fetch() default FetchType.EAGER;
164164

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

‎spec/src/main/asciidoc/ch11-metadata-for-or-mapping.adoc

+22-12
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,9 @@ strategy hint has been specified. In particular, lazy fetching might
575575
only be available for `Basic` mappings for which property-based access
576576
is used.
577577

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

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

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

2986+
If the annotated field or property is also annotated with
2987+
`jakarta.annotation.Nonnull`, then `optional=false` is implied
2988+
and the value of the `optional` annotation member is ignored.
2989+
29862990
.ManyToOne Annotation Elements
29872991
[#a15202,options="header"]
29882992
|===
@@ -3010,9 +3014,10 @@ persistence provider runtime.
30103014

30113015
|boolean
30123016
|optional
3013-
|(Optional) Whether the association is
3014-
optional. If set to false then a non-null relationship must always
3015-
exist.
3017+
|(Optional) Whether the association is optional. If set to false,
3018+
then a non-null relationship must always exist.
3019+
May be used in schema generation. Ignored if the field or property
3020+
is annotated `Nonnull`.
30163021
|true
30173022
|===
30183023

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

4126+
If the annotated field or property is also annotated with
4127+
`jakarta.annotation.Nonnull`, then `optional=false` is implied
4128+
and the value of the `optional` annotation member is ignored.
4129+
41214130
.OneToOne Annotation Elements
41224131
[#a15735,options="header"]
41234132
|===
@@ -4144,9 +4153,10 @@ persistence provider runtime.
41444153

41454154
|boolean
41464155
|optional
4147-
|(Optional) Whether the association is
4148-
optional. If set to false then a non-null relationship must always
4149-
exist.
4156+
|(Optional) Whether the association is optional. If set to false,
4157+
then a non-null relationship must always exist.
4158+
May be used in schema generation. Ignored if the field or property
4159+
is annotated `Nonnull`.
41504160
|true
41514161

41524162
|String

0 commit comments

Comments
 (0)
Please sign in to comment.