Skip to content

Commit

Permalink
Make the name member of @XxxxGenerator annotations optional (#416)
Browse files Browse the repository at this point in the history
See #406

Co-authored-by: Lukas Jungmann <[email protected]>
  • Loading branch information
gavinking and lukasj authored Aug 8, 2023
1 parent 254674c commit 4daf494
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
17 changes: 12 additions & 5 deletions api/src/main/java/jakarta/persistence/GeneratedValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

// Contributors:
// Gavin King - 3.2
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0

Expand All @@ -31,7 +32,7 @@
* may be applied to a primary key property or field of an entity or
* mapped superclass in conjunction with the {@link Id} annotation.
* The use of the <code>GeneratedValue</code> annotation is only
* required to be supported for simple primary keys. Use of the
* required to be supported for simple primary keys. Use of the
* <code>GeneratedValue</code> annotation is not supported for derived
* primary keys.
*
Expand Down Expand Up @@ -71,10 +72,16 @@
GenerationType strategy() default AUTO;

/**
* (Optional) The name of the primary key generator
* to use as specified in the {@link SequenceGenerator}
* or {@link TableGenerator} annotation.
* <p> Defaults to the id generator supplied by persistence provider.
* (Optional) The name of the primary key generator to
* use, as specified by the {@link SequenceGenerator}
* or {@link TableGenerator} annotation which declares
* the generator.
* <p> The name defaults to the entity name of the
* entity in which the annotation occurs.
* <p> If there is no generator with the defaulted
* name, then the persistence provider supplies a
* default id generator, of a type compatible with
* the value of the strategy member.
*/
String generator() default "";
}
11 changes: 9 additions & 2 deletions api/src/main/java/jakarta/persistence/SequenceGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

// Contributors:
// Gavin King - 3.2
// Lukas Jungmann - 2.2
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0
Expand All @@ -34,6 +35,10 @@
* generator name is global to the persistence unit (across all
* generator types).
*
* <p> If no name is explicitly specified, and the annotation occurs
* on an entity class or primary key attribute of an entity class,
* then the name defaults to the name of the entity.
*
* <pre>
* Example:
*
Expand All @@ -48,11 +53,13 @@
public @interface SequenceGenerator {

/**
* (Required) A unique generator name that can be referenced
* (Optional) A unique generator name that can be referenced
* by one or more classes to be the generator for primary key
* values.
* <p> Defaults to the name of the entity when the annotation
* occurs on an entity class or primary key attribute.
*/
String name();
String name() default "";

/**
* (Optional) The name of the database sequence object from
Expand Down
20 changes: 13 additions & 7 deletions api/src/main/java/jakarta/persistence/TableGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/

// Contributors:
// Gavin King - 3.2
// Lukas Jungmann - 2.2
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0


package jakarta.persistence;

import java.lang.annotation.Target;
Expand All @@ -35,14 +35,19 @@
* field or property. The scope of the generator name is global
* to the persistence unit (across all generator types).
*
* <p> If no name is explicitly specified, and the annotation
* occurs on an entity class or primary key attribute of an
* entity class, then the name defaults to the name of the
* entity.
*
* <pre>
* Example 1:
*
* &#064;Entity public class Employee {
* ...
* &#064;TableGenerator(
* name="empGen",
* table="ID_GEN",
* table="ID_GEN",
* pkColumnName="GEN_KEY",
* valueColumnName="GEN_VALUE",
* pkColumnValue="EMP_ID",
Expand All @@ -58,13 +63,12 @@
* &#064;Entity public class Address {
* ...
* &#064;TableGenerator(
* name="addressGen",
* table="ID_GEN",
* table="ID_GEN",
* pkColumnName="GEN_KEY",
* valueColumnName="GEN_VALUE",
* pkColumnValue="ADDR_ID")
* &#064;Id
* &#064;GeneratedValue(strategy=TABLE, generator="addressGen")
* &#064;GeneratedValue(strategy=TABLE)
* int id;
* ...
* }
Expand All @@ -80,10 +84,12 @@
public @interface TableGenerator {

/**
* (Required) A unique generator name that can be referenced
* (optional) A unique generator name that can be referenced
* by one or more classes to be the generator for id values.
* <p> Defaults to the name of the entity when the annotation
* occurs on an entity class or primary key attribute.
*/
String name();
String name() default "";

/**
* (Optional) Name of table that stores the generated id values.
Expand Down
3 changes: 3 additions & 0 deletions spec/src/main/asciidoc/appendixes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,6 @@ Added `||` string concatenation operator
Added _getSingleResultOrNull()_ to _Query_, _TypedQuery_, _StoredProcedureQuery_

Added _concat()_ overload accepting list of expressions to _CriteriaBuilder_

Made the _name_ member of _TableGenerator_ and _SequenceGenerator_ optional

14 changes: 12 additions & 2 deletions spec/src/main/asciidoc/ch11-metadata-for-or-mapping.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4687,6 +4687,11 @@ generator may be specified on the entity class or on the primary key
field or property. The scope of the generator name is global to the
persistence unit (across all generator types).

If no name is explicitly specified by the _SequenceGenerator_ annotation,
and the annotation occurs on an entity class or primary key attribute of
an entity class, then the name defaults to the name of the entity.
Otherwise, if the annotation occurs elsewhere, the behavior is undefined.

<<a16179>> lists the annotation elements
that may be specified for the _SequenceGenerator_ annotation and their
default values.
Expand All @@ -4697,7 +4702,7 @@ default values.
@Retention(RUNTIME)
@Repeatable(SequenceGenerators.class)
public @interface SequenceGenerator {
String name();
String name() default "";
String sequenceName() default "";
String catalog() default "";
String schema() default "";
Expand Down Expand Up @@ -4864,6 +4869,11 @@ generator may be specified on the entity class or on the primary key
field or property. The scope of the generator name is global to the
persistence unit (across all generator types).

If no name is explicitly specified by the _TableGenerator_ annotation,
and the annotation occurs on an entity class or primary key attribute
of an entity class, then the name defaults to the name of the entity.
Otherwise, if the annotation occurs elsewhere, the behavior is undefined.

<<a162771>> lists the annotation elements that
may be specified for the _TableGenerator_ annotation and their default
values.
Expand All @@ -4880,7 +4890,7 @@ primary key values are normally positive integers.
@Retention(RUNTIME)
@Repeatable(TableGenerators.class)
public @interface TableGenerator {
String name();
String name() default "";
String table() default "";
String catalog() default "";
String schema() default "";
Expand Down
4 changes: 2 additions & 2 deletions spec/src/main/asciidoc/ch12-xml-or-mapping-descriptor.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3048,7 +3048,7 @@ object/relational mapping schema for use with the persistence API.
@Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME)
public @interface SequenceGenerator {
String name();
String name() default "";
String sequenceName() default "";
String catalog() default "";
String schema() default "";
Expand Down Expand Up @@ -3157,7 +3157,7 @@ object/relational mapping schema for use with the persistence API.
@Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME)
public @interface TableGenerator {
String name();
String name() default "";
String table() default "";
String catalog() default "";
String schema() default "";
Expand Down

0 comments on commit 4daf494

Please sign in to comment.