10
10
import java .sql .PreparedStatement ;
11
11
import java .sql .ResultSet ;
12
12
import java .sql .SQLException ;
13
- import java .util .Collections ;
14
13
import java .util .Properties ;
15
14
16
15
import org .hibernate .HibernateException ;
17
16
import org .hibernate .MappingException ;
18
17
import org .hibernate .boot .model .naming .ObjectNameNormalizer ;
19
18
import org .hibernate .boot .model .relational .Database ;
20
- import org .hibernate .boot .model .relational .NamedAuxiliaryDatabaseObject ;
21
19
import org .hibernate .boot .model .relational .Namespace ;
22
20
import org .hibernate .boot .model .relational .QualifiedName ;
23
21
import org .hibernate .boot .model .relational .QualifiedNameParser ;
22
+ import org .hibernate .boot .model .relational .Sequence ;
24
23
import org .hibernate .dialect .Dialect ;
25
24
import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
26
25
import org .hibernate .engine .spi .SessionImplementor ;
26
+ import org .hibernate .internal .log .DeprecationLogger ;
27
27
import org .hibernate .internal .util .config .ConfigurationHelper ;
28
28
import org .hibernate .service .ServiceRegistry ;
29
29
import org .hibernate .type .Type ;
@@ -57,12 +57,14 @@ public class SequenceGenerator
57
57
/**
58
58
* The parameters parameter, appended to the create sequence DDL.
59
59
* For example (Oracle): <tt>INCREMENT BY 1 START WITH 1 MAXVALUE 100 NOCACHE</tt>.
60
+ *
61
+ * @deprecated No longer supported. To specify initial-value or increment use the
62
+ * org.hibernate.id.enhanced.SequenceStyleGenerator generator instead.
60
63
*/
61
64
public static final String PARAMETERS = "parameters" ;
62
65
63
- private QualifiedName qualifiedSequenceName ;
66
+ private QualifiedName logicalQualifiedSequenceName ;
64
67
private String sequenceName ;
65
- private String parameters ;
66
68
private Type identifierType ;
67
69
private String sql ;
68
70
@@ -81,20 +83,24 @@ public String getSequenceName() {
81
83
@ Override
82
84
@ SuppressWarnings ("StatementWithEmptyBody" )
83
85
public void configure (Type type , Properties params , ServiceRegistry serviceRegistry ) throws MappingException {
86
+ DeprecationLogger .DEPRECATION_LOGGER .deprecatedSequenceGenerator ( getClass ().getName () );
87
+
84
88
identifierType = type ;
85
- parameters = params .getProperty ( PARAMETERS );
86
89
87
- final JdbcEnvironment jdbcEnvironment = serviceRegistry .getService ( JdbcEnvironment .class );
88
- final Dialect dialect = jdbcEnvironment .getDialect ();
89
90
final ObjectNameNormalizer normalizer = ( ObjectNameNormalizer ) params .get ( IDENTIFIER_NORMALIZER );
90
- qualifiedSequenceName = QualifiedNameParser .INSTANCE .parse (
91
+ logicalQualifiedSequenceName = QualifiedNameParser .INSTANCE .parse (
91
92
ConfigurationHelper .getString ( SEQUENCE , params , "hibernate_sequence" ),
92
93
normalizer .normalizeIdentifierQuoting ( params .getProperty ( CATALOG ) ),
93
94
normalizer .normalizeIdentifierQuoting ( params .getProperty ( SCHEMA ) )
94
95
);
95
- sequenceName = jdbcEnvironment .getQualifiedObjectNameFormatter ().format ( qualifiedSequenceName , dialect );
96
96
97
- sql = dialect .getSequenceNextValString ( sequenceName );
97
+ if ( params .containsKey ( PARAMETERS ) ) {
98
+ LOG .warn (
99
+ "Use of 'parameters' config setting is no longer supported; " +
100
+ "to specify initial-value or increment use the " +
101
+ "org.hibernate.id.enhanced.SequenceStyleGenerator generator instead."
102
+ );
103
+ }
98
104
}
99
105
100
106
@ Override
@@ -140,11 +146,7 @@ protected IntegralDataTypeHolder buildHolder() {
140
146
@ Override
141
147
@ SuppressWarnings ( {"deprecation" })
142
148
public String [] sqlCreateStrings (Dialect dialect ) throws HibernateException {
143
- String [] ddl = dialect .getCreateSequenceStrings ( sequenceName );
144
- if ( parameters != null ) {
145
- ddl [ddl .length - 1 ] += ' ' + parameters ;
146
- }
147
- return ddl ;
149
+ return dialect .getCreateSequenceStrings ( sequenceName , 1 , 1 );
148
150
}
149
151
150
152
@ Override
@@ -164,22 +166,29 @@ public String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect d
164
166
165
167
@ Override
166
168
public void registerExportables (Database database ) {
167
- // we cannot register a proper Sequence object here because of the free-form
168
- //'parameters' as opposed to specific initialValue/increment values
169
-
170
169
final Namespace namespace = database .locateNamespace (
171
- qualifiedSequenceName .getCatalogName (),
172
- qualifiedSequenceName .getSchemaName ()
170
+ logicalQualifiedSequenceName .getCatalogName (),
171
+ logicalQualifiedSequenceName .getSchemaName ()
173
172
);
173
+ Sequence sequence = namespace .locateSequence ( logicalQualifiedSequenceName .getObjectName () );
174
+ if ( sequence != null ) {
175
+ sequence .validate ( 1 , 1 );
176
+ }
177
+ else {
178
+ sequence = namespace .createSequence (
179
+ logicalQualifiedSequenceName .getObjectName (),
180
+ 1 ,
181
+ 1
182
+ );
183
+ }
184
+
185
+ final JdbcEnvironment jdbcEnvironment = database .getJdbcEnvironment ();
186
+ final Dialect dialect = jdbcEnvironment .getDialect ();
174
187
175
- database .addAuxiliaryDatabaseObject (
176
- new NamedAuxiliaryDatabaseObject (
177
- qualifiedSequenceName .getObjectName ().render (),
178
- namespace ,
179
- sqlCreateStrings ( database .getDialect () ),
180
- sqlDropStrings ( database .getDialect () ),
181
- Collections .<String >emptySet ()
182
- )
188
+ this .sequenceName = jdbcEnvironment .getQualifiedObjectNameFormatter ().format (
189
+ sequence .getName (),
190
+ dialect
183
191
);
192
+ this .sql = jdbcEnvironment .getDialect ().getSequenceNextValString ( sequenceName );
184
193
}
185
194
}
0 commit comments