Historically Hibernate always expects that {{@GeneratedValue}} is matched with a matching named "generator definition". The name is provided by {{GeneratedValue#generator}}. The corresponding "generator definition" would be a {{@GenericGenerator}}, {{@SequenceGenerator}} or {{@TableGenerator}} based on the value of {{GeneratedValue#strategy}}:
h3. {{SEQUENCE}} and {{TABLE}}
Given {{@GeneratedValue( strategy=SEQUENCE, ... )}} Hibernate would look for a matching {{@GenericGenerator}} or a {{@SequenceGenerator}}. Given {{@GeneratedValue( strategy=TABLE, ... )}} Hibernate would look for a matching {{@GenericGenerator}} or a {{@TableGenerator}}.
Again, it would be an error if Hibernate did not find the matching {{@GenericGenerator}}, {{@SequenceGenerator}} or {{@TableGenerator}}. Ultimately though, these generator definitions are really just about handling non-default situations (e.g., use a specific increment size for a sequence/table, etc). But it should be ok for the user to leave them off and just get the "default behavior" (e.g. increment size of 50 as JPA defines for {{@SequenceGenerator}} and {{@TableGenerator}}).
*_NOTE: I will talk about SEQUENCE here, but the same holds true for TABLE_*
In other words, this should be a valid mapping:
{code} @Id @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "my_db_sequence" ) public Integer id; {code}
Here we would pick up the JPA defined defaults (see {{@SequenceGenerator}} and {{@TableGenerator}} for the defined defaults).
The name is an additional discussion, and is the same discussion for:
{code} @Id @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "my_db_sequence" ) @SequenceGenerator( name = "my_db_sequence", initialValue = 100, allocationSize = 500 ) public Integer id; {code}
In both cases we do not provide an explicit {{@SequenceGenerator#sequenceName}} value. Historically Hibernate interprets the default name for both sequence and table as {{"hibernate_sequence"}}. Ideally though (keeping with the theme of {{@SequenceGenerator}} and {{@TableGenerator}} defining just overrides) we'd want the sequence here to be named {{"my_db_sequence"}}.
So another part of the work here is to support this distinction - by a setting {{hibernate.model.generator_name_as_sequence_name}}. By default, this is {{true}} which means we will pick {{"my_db_sequence"}} ({{@GeneratedValue#generator}})); setting that to {{false}} indicates to pick the legacy {{"hibernate_sequence"}} name.
h3. {{IDENTITY}}
This discussion has no bearing on how {{GenerationType#IDENTITY}} is handled.
h3. {{AUTO}}
In keeping with "less typing == better", this also covers some cases related to {{GenerationType#AUTO}} specifically in how {{org.hibernate.boot.model.IdGeneratorStrategyInterpreter#determineGeneratorName}} can operate. This work changes the "parameter object" passed in to that method to give it access to the {{@GeneratedValue#generator}} value. At the moment, this only case this is used is to handle:
{code} @Id @GeneratedValue( strategy = GenerationType.AUTO, generator = "increment" ) public Integer id; {code}
Previously this mapping would not work. Instead a user would have to say:
{code} @Id @GeneratedValue( strategy = GenerationType.AUTO, generator = "increment" ) @GenericGenerator( name="increment", strategy="increment" ) public Integer id; {code}
"increment" generator is a Hibernate-specific, in-memory strategy performing simple incrementing for generating id values. Specifically it is a short-name for Hibernate's {{org.hibernate.id.IncrementGenerator}}. Generally speaking there is little need to configure {{IncrementGenerator}}, so requiring the {{@GenericGenerator}} is unnecessary in most cases. |
|