|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.boot.internal; |
| 8 | + |
| 9 | +import java.util.UUID; |
| 10 | + |
| 11 | +import org.hibernate.boot.model.IdentifierGeneratorDefinition; |
| 12 | +import org.hibernate.id.IncrementGenerator; |
| 13 | +import org.hibernate.id.PersistentIdentifierGenerator; |
| 14 | +import org.hibernate.id.UUIDGenerator; |
| 15 | +import org.hibernate.id.enhanced.SequenceStyleGenerator; |
| 16 | +import org.hibernate.internal.CoreLogging; |
| 17 | +import org.hibernate.internal.CoreMessageLogger; |
| 18 | + |
| 19 | +import jakarta.persistence.GeneratedValue; |
| 20 | +import jakarta.persistence.GenerationType; |
| 21 | +import jakarta.persistence.SequenceGenerator; |
| 22 | +import jakarta.persistence.TableGenerator; |
| 23 | + |
| 24 | +/** |
| 25 | + * Interpretations related to value generators based on the {@linkplain GenerationType strategy/type} |
| 26 | + * |
| 27 | + * @author Steve Ebersole |
| 28 | + */ |
| 29 | +public class GenerationStrategyInterpreter { |
| 30 | + private static final CoreMessageLogger LOG = CoreLogging.messageLogger( GenerationStrategyInterpreter.class ); |
| 31 | + public static final GenerationStrategyInterpreter STRATEGY_INTERPRETER = new GenerationStrategyInterpreter(); |
| 32 | + |
| 33 | + public interface GeneratorNameDeterminationContext { |
| 34 | + /** |
| 35 | + * The Java type of the attribute defining the id whose value is to |
| 36 | + * be generated. |
| 37 | + */ |
| 38 | + Class<?> getIdType(); |
| 39 | + |
| 40 | + /** |
| 41 | + * The {@link GeneratedValue#generator()} name. |
| 42 | + */ |
| 43 | + String getGeneratedValueGeneratorName(); |
| 44 | + } |
| 45 | + |
| 46 | + private GenerationStrategyInterpreter() { |
| 47 | + } |
| 48 | + |
| 49 | + public String determineGeneratorName(GenerationType generationType, GeneratorNameDeterminationContext context) { |
| 50 | + switch ( generationType ) { |
| 51 | + case IDENTITY: { |
| 52 | + return "identity"; |
| 53 | + } |
| 54 | + case SEQUENCE: { |
| 55 | + return SequenceStyleGenerator.class.getName(); |
| 56 | + } |
| 57 | + case TABLE: { |
| 58 | + return org.hibernate.id.enhanced.TableGenerator.class.getName(); |
| 59 | + } |
| 60 | + case UUID: { |
| 61 | + return UUIDGenerator.class.getName(); |
| 62 | + } |
| 63 | + case AUTO: { |
| 64 | + if ( UUID.class.isAssignableFrom( context.getIdType() ) ) { |
| 65 | + return UUIDGenerator.class.getName(); |
| 66 | + } |
| 67 | + else if ( "increment".equalsIgnoreCase( context.getGeneratedValueGeneratorName() ) ) { |
| 68 | + // special case for @GeneratedValue(name="increment") |
| 69 | + // for some reason we consider there to be an implicit |
| 70 | + // generator named 'increment' (doesn't seem very useful) |
| 71 | + return IncrementGenerator.class.getName(); |
| 72 | + } |
| 73 | + else { |
| 74 | + return SequenceStyleGenerator.class.getName(); |
| 75 | + } |
| 76 | + } |
| 77 | + default: { |
| 78 | + throw new UnsupportedOperationException( "Unsupported generation type:" + generationType ); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void interpretTableGenerator( |
| 84 | + TableGenerator tableGeneratorAnnotation, |
| 85 | + IdentifierGeneratorDefinition.Builder definitionBuilder) { |
| 86 | + definitionBuilder.setName( tableGeneratorAnnotation.name() ); |
| 87 | + definitionBuilder.setStrategy( org.hibernate.id.enhanced.TableGenerator.class.getName() ); |
| 88 | + definitionBuilder.addParam( org.hibernate.id.enhanced.TableGenerator.CONFIG_PREFER_SEGMENT_PER_ENTITY, "true" ); |
| 89 | + |
| 90 | + if ( !tableGeneratorAnnotation.catalog().isEmpty()) { |
| 91 | + definitionBuilder.addParam( PersistentIdentifierGenerator.CATALOG, tableGeneratorAnnotation.catalog() ); |
| 92 | + } |
| 93 | + if ( !tableGeneratorAnnotation.schema().isEmpty()) { |
| 94 | + definitionBuilder.addParam( PersistentIdentifierGenerator.SCHEMA, tableGeneratorAnnotation.schema() ); |
| 95 | + } |
| 96 | + if ( !tableGeneratorAnnotation.table().isEmpty()) { |
| 97 | + definitionBuilder.addParam( |
| 98 | + org.hibernate.id.enhanced.TableGenerator.TABLE_PARAM, |
| 99 | + tableGeneratorAnnotation.table() |
| 100 | + ); |
| 101 | + } |
| 102 | + if ( !tableGeneratorAnnotation.pkColumnName().isEmpty()) { |
| 103 | + definitionBuilder.addParam( |
| 104 | + org.hibernate.id.enhanced.TableGenerator.SEGMENT_COLUMN_PARAM, |
| 105 | + tableGeneratorAnnotation.pkColumnName() |
| 106 | + ); |
| 107 | + } |
| 108 | + if ( !tableGeneratorAnnotation.pkColumnValue().isEmpty()) { |
| 109 | + definitionBuilder.addParam( |
| 110 | + org.hibernate.id.enhanced.TableGenerator.SEGMENT_VALUE_PARAM, |
| 111 | + tableGeneratorAnnotation.pkColumnValue() |
| 112 | + ); |
| 113 | + } |
| 114 | + if ( !tableGeneratorAnnotation.valueColumnName().isEmpty()) { |
| 115 | + definitionBuilder.addParam( |
| 116 | + org.hibernate.id.enhanced.TableGenerator.VALUE_COLUMN_PARAM, |
| 117 | + tableGeneratorAnnotation.valueColumnName() |
| 118 | + ); |
| 119 | + } |
| 120 | + definitionBuilder.addParam( |
| 121 | + org.hibernate.id.enhanced.TableGenerator.INCREMENT_PARAM, |
| 122 | + String.valueOf( tableGeneratorAnnotation.allocationSize() ) |
| 123 | + ); |
| 124 | + // See comment on HHH-4884 wrt initialValue. Basically initialValue is really the stated value + 1 |
| 125 | + definitionBuilder.addParam( |
| 126 | + org.hibernate.id.enhanced.TableGenerator.INITIAL_PARAM, |
| 127 | + String.valueOf( tableGeneratorAnnotation.initialValue() + 1 ) |
| 128 | + ); |
| 129 | + |
| 130 | + // TODO : implement unique-constraint support |
| 131 | + if ( tableGeneratorAnnotation.uniqueConstraints() != null |
| 132 | + && tableGeneratorAnnotation.uniqueConstraints().length > 0 ) { |
| 133 | + LOG.ignoringTableGeneratorConstraints( tableGeneratorAnnotation.name() ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public void interpretSequenceGenerator( |
| 138 | + SequenceGenerator sequenceGeneratorAnnotation, |
| 139 | + IdentifierGeneratorDefinition.Builder definitionBuilder) { |
| 140 | + definitionBuilder.setName( sequenceGeneratorAnnotation.name() ); |
| 141 | + definitionBuilder.setStrategy( SequenceStyleGenerator.class.getName() ); |
| 142 | + |
| 143 | + if ( !sequenceGeneratorAnnotation.catalog().isEmpty()) { |
| 144 | + definitionBuilder.addParam( |
| 145 | + PersistentIdentifierGenerator.CATALOG, |
| 146 | + sequenceGeneratorAnnotation.catalog() |
| 147 | + ); |
| 148 | + } |
| 149 | + if ( !sequenceGeneratorAnnotation.schema().isEmpty()) { |
| 150 | + definitionBuilder.addParam( |
| 151 | + PersistentIdentifierGenerator.SCHEMA, |
| 152 | + sequenceGeneratorAnnotation.schema() |
| 153 | + ); |
| 154 | + } |
| 155 | + if ( !sequenceGeneratorAnnotation.sequenceName().isEmpty()) { |
| 156 | + definitionBuilder.addParam( |
| 157 | + SequenceStyleGenerator.SEQUENCE_PARAM, |
| 158 | + sequenceGeneratorAnnotation.sequenceName() |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + definitionBuilder.addParam( |
| 163 | + SequenceStyleGenerator.INCREMENT_PARAM, |
| 164 | + String.valueOf( sequenceGeneratorAnnotation.allocationSize() ) |
| 165 | + ); |
| 166 | + definitionBuilder.addParam( |
| 167 | + SequenceStyleGenerator.INITIAL_PARAM, |
| 168 | + String.valueOf( sequenceGeneratorAnnotation.initialValue() ) |
| 169 | + ); |
| 170 | + } |
| 171 | +} |
0 commit comments