-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-18620 - Add @NativeGenerator #8987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
hibernate-core/src/main/java/org/hibernate/annotations/NativeGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.annotations; | ||
|
|
||
| import java.lang.annotation.Target; | ||
| import java.lang.annotation.Retention; | ||
|
|
||
| import org.hibernate.Incubating; | ||
| import org.hibernate.dialect.Dialect; | ||
|
|
||
| import jakarta.persistence.SequenceGenerator; | ||
| import jakarta.persistence.TableGenerator; | ||
|
|
||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.PACKAGE; | ||
| import static java.lang.annotation.ElementType.TYPE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| /** | ||
| * Generator that picks a strategy based on the {@linkplain Dialect#getNativeValueGenerationStrategy() dialect}. | ||
| * | ||
| * @since 7.0 | ||
| * @author Steve Ebersole | ||
| */ | ||
| @Target({METHOD, FIELD, TYPE, PACKAGE}) | ||
| @Retention(RUNTIME) | ||
| @IdGeneratorType(org.hibernate.id.NativeGenerator.class) | ||
| @Incubating | ||
| public @interface NativeGenerator { | ||
| SequenceGenerator sequenceForm() default @SequenceGenerator(); | ||
| TableGenerator tableForm() default @TableGenerator(); | ||
| } | ||
198 changes: 198 additions & 0 deletions
198
...re/src/main/java/org/hibernate/boot/model/internal/AbstractEntityIdGeneratorResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| /* | ||
| * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.boot.model.internal; | ||
|
|
||
| import java.lang.annotation.Annotation; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.hibernate.MappingException; | ||
| import org.hibernate.annotations.IdGeneratorType; | ||
| import org.hibernate.boot.model.IdentifierGeneratorDefinition; | ||
| import org.hibernate.boot.model.relational.Database; | ||
| import org.hibernate.boot.spi.MetadataBuildingContext; | ||
| import org.hibernate.dialect.Dialect; | ||
| import org.hibernate.generator.Generator; | ||
| import org.hibernate.internal.util.collections.CollectionHelper; | ||
| import org.hibernate.mapping.Column; | ||
| import org.hibernate.mapping.PersistentClass; | ||
| import org.hibernate.mapping.SimpleValue; | ||
| import org.hibernate.models.spi.AnnotationTarget; | ||
| import org.hibernate.models.spi.ClassDetails; | ||
| import org.hibernate.models.spi.MemberDetails; | ||
|
|
||
| import jakarta.persistence.GeneratedValue; | ||
|
|
||
| import static org.hibernate.boot.model.internal.GeneratorAnnotationHelper.handleIdGeneratorType; | ||
| import static org.hibernate.boot.model.internal.GeneratorParameters.identityTablesString; | ||
| import static org.hibernate.boot.model.internal.GeneratorStrategies.mapLegacyNamedGenerator; | ||
| import static org.hibernate.id.IdentifierGenerator.ENTITY_NAME; | ||
| import static org.hibernate.id.IdentifierGenerator.JPA_ENTITY_NAME; | ||
| import static org.hibernate.id.OptimizableGenerator.IMPLICIT_NAME_BASE; | ||
| import static org.hibernate.id.PersistentIdentifierGenerator.PK; | ||
| import static org.hibernate.id.PersistentIdentifierGenerator.TABLE; | ||
| import static org.hibernate.id.PersistentIdentifierGenerator.TABLES; | ||
|
|
||
| /** | ||
| * Template support for IdGeneratorResolver implementations dealing with entity identifiers | ||
| * | ||
| * @author Steve Ebersole | ||
| */ | ||
| public abstract class AbstractEntityIdGeneratorResolver implements IdGeneratorResolver { | ||
| protected final PersistentClass entityMapping; | ||
| protected final SimpleValue idValue; | ||
| protected final MemberDetails idMember; | ||
| protected final GeneratedValue generatedValue; | ||
| protected final MetadataBuildingContext buildingContext; | ||
|
|
||
| public AbstractEntityIdGeneratorResolver( | ||
| PersistentClass entityMapping, | ||
| SimpleValue idValue, | ||
| MemberDetails idMember, | ||
| GeneratedValue generatedValue, | ||
| MetadataBuildingContext buildingContext) { | ||
| this.entityMapping = entityMapping; | ||
| this.idValue = idValue; | ||
| this.idMember = idMember; | ||
| this.generatedValue = generatedValue; | ||
| this.buildingContext = buildingContext; | ||
| } | ||
|
|
||
| @Override | ||
| public final void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException { | ||
| switch ( generatedValue.strategy() ) { | ||
| case UUID -> GeneratorAnnotationHelper.handleUuidStrategy( idValue, idMember, buildingContext ); | ||
| case IDENTITY -> GeneratorAnnotationHelper.handleIdentityStrategy( idValue ); | ||
| case SEQUENCE -> handleSequenceStrategy(); | ||
| case TABLE -> handleTableStrategy(); | ||
| case AUTO -> handleAutoStrategy(); | ||
| } | ||
| } | ||
|
|
||
| private void handleSequenceStrategy() { | ||
| if ( generatedValue.generator().isEmpty() ) { | ||
| handleUnnamedSequenceGenerator(); | ||
| } | ||
| else { | ||
| handleNamedSequenceGenerator(); | ||
| } | ||
| } | ||
|
|
||
| protected abstract void handleUnnamedSequenceGenerator(); | ||
|
|
||
| protected abstract void handleNamedSequenceGenerator(); | ||
|
|
||
| private void handleTableStrategy() { | ||
| if ( generatedValue.generator().isEmpty() ) { | ||
| handleUnnamedTableGenerator(); | ||
| } | ||
| else { | ||
| handleNamedTableGenerator(); | ||
| } | ||
| } | ||
|
|
||
| protected abstract void handleUnnamedTableGenerator(); | ||
|
|
||
| protected abstract void handleNamedTableGenerator(); | ||
|
|
||
| private void handleAutoStrategy() { | ||
| if ( generatedValue.generator().isEmpty() ) { | ||
| handleUnnamedAutoGenerator(); | ||
| } | ||
| else { | ||
| handleNamedAutoGenerator(); | ||
| } | ||
| } | ||
|
|
||
| protected abstract void handleUnnamedAutoGenerator(); | ||
|
|
||
| protected abstract void handleNamedAutoGenerator(); | ||
|
|
||
| protected boolean handleAsMetaAnnotated() { | ||
| final Annotation fromMember = findGeneratorAnnotation( idMember ); | ||
| if ( fromMember != null ) { | ||
| handleIdGeneratorType( fromMember, idValue, idMember, buildingContext ); | ||
| return true; | ||
| } | ||
|
|
||
| final Annotation fromClass = findGeneratorAnnotation( idMember.getDeclaringType() ); | ||
| if ( fromClass != null ) { | ||
| handleIdGeneratorType( fromClass, idValue, idMember, buildingContext ); | ||
| return true; | ||
| } | ||
|
|
||
| final ClassDetails packageInfoDetails = GeneratorAnnotationHelper.locatePackageInfoDetails( idMember.getDeclaringType(), buildingContext ); | ||
| if ( packageInfoDetails != null ) { | ||
| final Annotation fromPackage = findGeneratorAnnotation( packageInfoDetails ); | ||
| if ( fromPackage != null ) { | ||
| handleIdGeneratorType( fromPackage, idValue, idMember, buildingContext ); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private Annotation findGeneratorAnnotation(AnnotationTarget annotationTarget) { | ||
| final List<? extends Annotation> metaAnnotated = annotationTarget.getMetaAnnotated( IdGeneratorType.class, buildingContext.getMetadataCollector().getSourceModelBuildingContext() ); | ||
| if ( CollectionHelper.size( metaAnnotated ) > 0 ) { | ||
| return metaAnnotated.get( 0 ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| protected boolean handleAsLegacyGenerator() { | ||
| // Handle a few legacy Hibernate generators... | ||
| final String nameFromGeneratedValue = generatedValue.generator(); | ||
| if ( !nameFromGeneratedValue.isEmpty() ) { | ||
| final Class<? extends Generator> legacyNamedGenerator = mapLegacyNamedGenerator( nameFromGeneratedValue, idValue ); | ||
| if ( legacyNamedGenerator != null ) { | ||
| final Map<String,String> configuration = buildLegacyGeneratorConfig(); | ||
| //noinspection unchecked,rawtypes | ||
| GeneratorBinder.createGeneratorFrom( | ||
| new IdentifierGeneratorDefinition( nameFromGeneratedValue, legacyNamedGenerator.getName(), configuration ), | ||
| idValue, | ||
| (Map) configuration, | ||
| buildingContext | ||
| ); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private HashMap<String, String> buildLegacyGeneratorConfig() { | ||
| final Database database = buildingContext.getMetadataCollector().getDatabase(); | ||
| final Dialect dialect = database.getDialect(); | ||
|
|
||
| final HashMap<String, String> configuration = new HashMap<>(); | ||
|
|
||
| final String tableName = idValue.getTable().getQuotedName( dialect ); | ||
| configuration.put( TABLE, tableName ); | ||
|
|
||
| final Column idColumn = (Column) idValue.getSelectables().get( 0); | ||
| final String idColumnName = idColumn.getQuotedName( dialect ); | ||
| configuration.put( PK, idColumnName ); | ||
|
|
||
| configuration.put( ENTITY_NAME, entityMapping.getEntityName() ); | ||
| configuration.put( JPA_ENTITY_NAME, entityMapping.getJpaEntityName() ); | ||
|
|
||
| // The table name is not really a good default for subselect entities, | ||
| // so use the JPA entity name which is short | ||
| configuration.put( | ||
| IMPLICIT_NAME_BASE, | ||
| idValue.getTable().isSubselect() | ||
| ? entityMapping.getJpaEntityName() | ||
| : idValue.getTable().getName() | ||
| ); | ||
|
|
||
| configuration.put( TABLES, identityTablesString( dialect, entityMapping.getRootClass() ) ); | ||
|
|
||
| return configuration; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.