135
135
import org .hibernate .service .internal .StandardServiceRegistryImpl ;
136
136
import org .hibernate .tool .hbm2ddl .DatabaseMetadata ;
137
137
import org .hibernate .tool .hbm2ddl .IndexMetadata ;
138
+ import org .hibernate .tool .hbm2ddl .SchemaUpdateScript ;
138
139
import org .hibernate .tool .hbm2ddl .TableMetadata ;
140
+ import org .hibernate .tool .hbm2ddl .UniqueConstraintSchemaUpdateStrategy ;
139
141
import org .hibernate .tuple .entity .EntityTuplizerFactory ;
140
142
import org .hibernate .type .BasicType ;
141
143
import org .hibernate .type .SerializationException ;
@@ -1110,57 +1112,64 @@ public String[] generateSchemaCreationScript(Dialect dialect) throws HibernateEx
1110
1112
*
1111
1113
* @throws HibernateException Generally indicates a problem calling {@link #buildMappings()}
1112
1114
*
1113
- * @see org.hibernate.tool.hbm2ddl.SchemaExport
1115
+ * @see org.hibernate.tool.hbm2ddl.SchemaUpdate
1116
+ *
1117
+ * @deprecated Use {@link #generateSchemaUpdateScriptList(Dialect, DatabaseMetadata)} instead
1114
1118
*/
1115
1119
@ SuppressWarnings ({ "unchecked" })
1120
+ @ Deprecated
1116
1121
public String [] generateSchemaUpdateScript (Dialect dialect , DatabaseMetadata databaseMetadata )
1117
1122
throws HibernateException {
1123
+ List <SchemaUpdateScript > scripts = generateSchemaUpdateScriptList ( dialect , databaseMetadata );
1124
+ return SchemaUpdateScript .toStringArray ( scripts );
1125
+ }
1126
+
1127
+ /**
1128
+ * @param dialect The dialect for which to generate the creation script
1129
+ * @param databaseMetadata The database catalog information for the database to be updated; needed to work out what
1130
+ * should be created/altered
1131
+ *
1132
+ * @return The sequence of DDL commands to apply the schema objects
1133
+ *
1134
+ * @throws HibernateException Generally indicates a problem calling {@link #buildMappings()}
1135
+ *
1136
+ * @see org.hibernate.tool.hbm2ddl.SchemaUpdate
1137
+ */
1138
+ public List <SchemaUpdateScript > generateSchemaUpdateScriptList (Dialect dialect , DatabaseMetadata databaseMetadata )
1139
+ throws HibernateException {
1118
1140
secondPassCompile ();
1119
1141
1120
1142
String defaultCatalog = properties .getProperty ( Environment .DEFAULT_CATALOG );
1121
1143
String defaultSchema = properties .getProperty ( Environment .DEFAULT_SCHEMA );
1144
+ UniqueConstraintSchemaUpdateStrategy constraintMethod = UniqueConstraintSchemaUpdateStrategy .interpret ( properties
1145
+ .get ( Environment .UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY ) );
1122
1146
1123
- ArrayList < String > script = new ArrayList <String >( 50 );
1147
+ List < SchemaUpdateScript > scripts = new ArrayList <SchemaUpdateScript >( );
1124
1148
1125
1149
Iterator iter = getTableMappings ();
1126
1150
while ( iter .hasNext () ) {
1127
1151
Table table = (Table ) iter .next ();
1128
- String tableSchema = ( table .getSchema () == null ) ? defaultSchema : table .getSchema () ;
1152
+ String tableSchema = ( table .getSchema () == null ) ? defaultSchema : table .getSchema ();
1129
1153
String tableCatalog = ( table .getCatalog () == null ) ? defaultCatalog : table .getCatalog ();
1130
1154
if ( table .isPhysicalTable () ) {
1131
1155
1132
- TableMetadata tableInfo = databaseMetadata .getTableMetadata (
1133
- table .getName (),
1134
- tableSchema ,
1135
- tableCatalog ,
1136
- table .isQuoted ()
1137
- );
1156
+ TableMetadata tableInfo = databaseMetadata .getTableMetadata ( table .getName (), tableSchema ,
1157
+ tableCatalog , table .isQuoted () );
1138
1158
if ( tableInfo == null ) {
1139
- script .add (
1140
- table .sqlCreateString (
1141
- dialect ,
1142
- mapping ,
1143
- tableCatalog ,
1144
- tableSchema
1145
- )
1146
- );
1159
+ scripts .add ( new SchemaUpdateScript ( table .sqlCreateString ( dialect , mapping , tableCatalog ,
1160
+ tableSchema ), false ) );
1147
1161
}
1148
1162
else {
1149
- Iterator <String > subiter = table .sqlAlterStrings (
1150
- dialect ,
1151
- mapping ,
1152
- tableInfo ,
1153
- tableCatalog ,
1154
- tableSchema
1155
- );
1163
+ Iterator <String > subiter = table .sqlAlterStrings ( dialect , mapping , tableInfo , tableCatalog ,
1164
+ tableSchema );
1156
1165
while ( subiter .hasNext () ) {
1157
- script .add ( subiter .next () );
1166
+ scripts .add ( new SchemaUpdateScript ( subiter .next (), false ) );
1158
1167
}
1159
1168
}
1160
1169
1161
1170
Iterator <String > comments = table .sqlCommentStrings ( dialect , defaultCatalog , defaultSchema );
1162
1171
while ( comments .hasNext () ) {
1163
- script .add ( comments .next () );
1172
+ scripts .add ( new SchemaUpdateScript ( comments .next (), false ) );
1164
1173
}
1165
1174
1166
1175
}
@@ -1169,55 +1178,47 @@ public String[] generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata dat
1169
1178
iter = getTableMappings ();
1170
1179
while ( iter .hasNext () ) {
1171
1180
Table table = (Table ) iter .next ();
1172
- String tableSchema = ( table .getSchema () == null ) ? defaultSchema : table .getSchema () ;
1181
+ String tableSchema = ( table .getSchema () == null ) ? defaultSchema : table .getSchema ();
1173
1182
String tableCatalog = ( table .getCatalog () == null ) ? defaultCatalog : table .getCatalog ();
1174
1183
if ( table .isPhysicalTable () ) {
1175
1184
1176
- TableMetadata tableInfo = databaseMetadata .getTableMetadata (
1177
- table .getName (),
1178
- tableSchema ,
1179
- tableCatalog ,
1180
- table .isQuoted ()
1181
- );
1182
-
1183
- Iterator uniqueIter = table .getUniqueKeyIterator ();
1184
- while ( uniqueIter .hasNext () ) {
1185
- final UniqueKey uniqueKey = (UniqueKey ) uniqueIter .next ();
1186
- // Skip if index already exists. Most of the time, this
1187
- // won't work since most Dialects use Constraints. However,
1188
- // keep it for the few that do use Indexes.
1189
- if ( tableInfo != null && StringHelper .isNotEmpty ( uniqueKey .getName () ) ) {
1190
- final IndexMetadata meta = tableInfo .getIndexMetadata ( uniqueKey .getName () );
1191
- if ( meta != null ) {
1192
- continue ;
1185
+ TableMetadata tableInfo = databaseMetadata .getTableMetadata ( table .getName (), tableSchema ,
1186
+ tableCatalog , table .isQuoted () );
1187
+
1188
+ if (! constraintMethod .equals ( UniqueConstraintSchemaUpdateStrategy .SKIP )) {
1189
+ Iterator uniqueIter = table .getUniqueKeyIterator ();
1190
+ while ( uniqueIter .hasNext () ) {
1191
+ final UniqueKey uniqueKey = (UniqueKey ) uniqueIter .next ();
1192
+ // Skip if index already exists. Most of the time, this
1193
+ // won't work since most Dialects use Constraints. However,
1194
+ // keep it for the few that do use Indexes.
1195
+ if ( tableInfo != null && StringHelper .isNotEmpty ( uniqueKey .getName () ) ) {
1196
+ final IndexMetadata meta = tableInfo .getIndexMetadata ( uniqueKey .getName () );
1197
+ if ( meta != null ) {
1198
+ continue ;
1199
+ }
1193
1200
}
1201
+ String constraintString = uniqueKey .sqlCreateString ( dialect , mapping , tableCatalog , tableSchema );
1202
+ if ( constraintString != null && !constraintString .isEmpty () )
1203
+ if ( constraintMethod .equals ( UniqueConstraintSchemaUpdateStrategy .DROP_RECREATE_QUIETLY ) ) {
1204
+ String constraintDropString = uniqueKey .sqlDropString ( dialect , tableCatalog , tableCatalog );
1205
+ scripts .add ( new SchemaUpdateScript ( constraintDropString , true ) );
1206
+ }
1207
+ scripts .add ( new SchemaUpdateScript ( constraintString , true ) );
1194
1208
}
1195
- String constraintString = uniqueKey .sqlCreateString ( dialect ,
1196
- mapping , tableCatalog , tableSchema );
1197
- if (constraintString != null ) script .add ( constraintString );
1198
1209
}
1199
1210
1200
1211
if ( dialect .hasAlterTable () ) {
1201
1212
Iterator subIter = table .getForeignKeyIterator ();
1202
1213
while ( subIter .hasNext () ) {
1203
1214
ForeignKey fk = (ForeignKey ) subIter .next ();
1204
1215
if ( fk .isPhysicalConstraint () ) {
1205
- boolean create = tableInfo == null || (
1206
- tableInfo .getForeignKeyMetadata ( fk ) == null && (
1207
- //Icky workaround for MySQL bug:
1208
- !( dialect instanceof MySQLDialect ) ||
1209
- tableInfo .getIndexMetadata ( fk .getName () ) == null
1210
- )
1211
- );
1216
+ boolean create = tableInfo == null || ( tableInfo .getForeignKeyMetadata ( fk ) == null && (
1217
+ // Icky workaround for MySQL bug:
1218
+ !( dialect instanceof MySQLDialect ) || tableInfo .getIndexMetadata ( fk .getName () ) == null ) );
1212
1219
if ( create ) {
1213
- script .add (
1214
- fk .sqlCreateString (
1215
- dialect ,
1216
- mapping ,
1217
- tableCatalog ,
1218
- tableSchema
1219
- )
1220
- );
1220
+ scripts .add ( new SchemaUpdateScript ( fk .sqlCreateString ( dialect , mapping ,
1221
+ tableCatalog , tableSchema ), false ) );
1221
1222
}
1222
1223
}
1223
1224
}
@@ -1233,14 +1234,8 @@ public String[] generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata dat
1233
1234
continue ;
1234
1235
}
1235
1236
}
1236
- script .add (
1237
- index .sqlCreateString (
1238
- dialect ,
1239
- mapping ,
1240
- tableCatalog ,
1241
- tableSchema
1242
- )
1243
- );
1237
+ scripts .add ( new SchemaUpdateScript ( index .sqlCreateString ( dialect , mapping , tableCatalog ,
1238
+ tableSchema ), false ) );
1244
1239
}
1245
1240
}
1246
1241
}
@@ -1251,11 +1246,11 @@ public String[] generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata dat
1251
1246
Object key = generator .generatorKey ();
1252
1247
if ( !databaseMetadata .isSequence ( key ) && !databaseMetadata .isTable ( key ) ) {
1253
1248
String [] lines = generator .sqlCreateStrings ( dialect );
1254
- script .addAll ( Arrays . asList ( lines ) );
1249
+ scripts .addAll ( SchemaUpdateScript . fromStringArray ( lines , false ) );
1255
1250
}
1256
1251
}
1257
1252
1258
- return ArrayHelper . toStringArray ( script ) ;
1253
+ return scripts ;
1259
1254
}
1260
1255
1261
1256
public void validateSchema (Dialect dialect , DatabaseMetadata databaseMetadata )throws HibernateException {
0 commit comments