Skip to content

Commit 1915de2

Browse files
committed
HHH-20372 support new JPA4 DDL features of @Index + @table
1 parent 4790fcb commit 1915de2

10 files changed

Lines changed: 189 additions & 8 deletions

File tree

‎hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ private static void bindJoinedTableAssociation(
667667
final JoinColumn[] joins;
668668
final JoinColumn[] inverseJoins;
669669
final Index[] jpaIndexes;
670+
final String type;
670671
final String options;
671672

672673
//JPA 2 has priority
@@ -678,6 +679,7 @@ private static void bindJoinedTableAssociation(
678679
joins = collectionTable.joinColumns();
679680
inverseJoins = null;
680681
jpaIndexes = collectionTable.indexes();
682+
type = collectionTable.type();
681683
options = collectionTable.options();
682684
}
683685
else {
@@ -688,6 +690,7 @@ private static void bindJoinedTableAssociation(
688690
joins = assocTable.joinColumns();
689691
inverseJoins = assocTable.inverseJoinColumns();
690692
jpaIndexes = assocTable.indexes();
693+
type = assocTable.type();
691694
options = assocTable.options();
692695
}
693696

@@ -706,6 +709,7 @@ private static void bindJoinedTableAssociation(
706709
}
707710
associationTableBinder.setUniqueConstraints( uniqueConstraints );
708711
associationTableBinder.setJpaIndex( jpaIndexes );
712+
associationTableBinder.setType( type );
709713
associationTableBinder.setOptions( options );
710714
//set check constraint in the second pass
711715
annJoins = ArrayHelper.isEmpty( joins ) ? null : joins;

‎hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ private void processComplementaryTableDefinitions() {
458458
TableBinder.addJpaIndexes( table, jpaTableUsage.indexes(), context );
459459
TableBinder.addTableCheck( table, jpaTableUsage.check() );
460460
TableBinder.addTableComment( table, jpaTableUsage.comment() );
461+
TableBinder.addTableType( table, jpaTableUsage.type() );
461462
TableBinder.addTableOptions( table, jpaTableUsage.options() );
462463
}
463464

@@ -2201,6 +2202,7 @@ public Join addJoinTable(JoinTable joinTable, PropertyHolder holder, boolean noD
22012202
final var table = join.getTable();
22022203
TableBinder.addTableCheck( table, joinTable.check() );
22032204
TableBinder.addTableComment( table, joinTable.comment() );
2205+
TableBinder.addTableType( table, joinTable.type() );
22042206
TableBinder.addTableOptions( table, joinTable.options() );
22052207
return join;
22062208
}
@@ -2220,6 +2222,7 @@ public Join addSecondaryTable(SecondaryTable secondaryTable, PropertyHolder hold
22202222
new IndexBinder( context ).bindIndexes( table, secondaryTable.indexes() );
22212223
TableBinder.addTableCheck( table, secondaryTable.check() );
22222224
TableBinder.addTableComment( table, secondaryTable.comment() );
2225+
TableBinder.addTableType( table, secondaryTable.type() );
22232226
TableBinder.addTableOptions( table, secondaryTable.options() );
22242227
return join;
22252228
}

‎hibernate-core/src/main/java/org/hibernate/boot/model/internal/ImplicitToOneJoinTableSecondPass.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ private TableBinder createTableBinder() {
102102

103103
tableBinder.setUniqueConstraints( joinTable.uniqueConstraints() );
104104
tableBinder.setJpaIndex( joinTable.indexes() );
105+
tableBinder.setType( joinTable.type() );
105106
tableBinder.setOptions( joinTable.options() );
106107

107108
return tableBinder;

‎hibernate-core/src/main/java/org/hibernate/boot/model/internal/IndexBinder.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static java.util.Collections.emptyList;
3232
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
3333
import static org.hibernate.internal.util.StringHelper.isEmpty;
34+
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
3435
import static org.hibernate.internal.util.collections.CollectionHelper.arrayList;
3536

3637
/**
@@ -160,6 +161,8 @@ private void createIndexOrUniqueKey(
160161
String[] orderings,
161162
boolean unique,
162163
boolean declaredAsIndex,
164+
String type,
165+
String using,
163166
String options,
164167
Selectable[] columns) {
165168
final var source =
@@ -171,7 +174,8 @@ private void createIndexOrUniqueKey(
171174
}
172175
}
173176
final var dialect = getDialect();
174-
if ( unique && !hasFormula && dialect.supportsUniqueConstraints() ) {
177+
if ( unique && !hasFormula && dialect.supportsUniqueConstraints()
178+
&& isEmpty( type ) && isEmpty( using ) ) {
175179
final String keyName = getImplicitNamingStrategy().determineUniqueKeyName( source ).render( dialect );
176180
final UniqueKey uniqueKey = table.getOrCreateUniqueKey( keyName );
177181
uniqueKey.setExplicit( true );
@@ -190,6 +194,12 @@ private void createIndexOrUniqueKey(
190194
final String keyName = getImplicitNamingStrategy().determineIndexName( source ).render( dialect );
191195
final Index index = table.getOrCreateIndex( keyName );
192196
index.setUnique( unique );
197+
if ( isNotEmpty( type ) ) {
198+
index.setType( type );
199+
}
200+
if ( isNotEmpty( using ) ) {
201+
index.setUsing( using );
202+
}
193203
index.setOptions( options );
194204
for ( int i = 0; i < columns.length; i++ ) {
195205
index.addColumn( columns[i], orderings != null ? orderings[i] : null );
@@ -212,6 +222,8 @@ void bindIndexes(Table table, jakarta.persistence.Index[] indexes) {
212222
initializeColumns( columnExpressions, ordering, parsed );
213223
final String name = index.name();
214224
final boolean unique = index.unique();
225+
final String type = index.type();
226+
final String using = index.using();
215227
final String options = index.options();
216228
createIndexOrUniqueKey(
217229
table,
@@ -221,6 +233,8 @@ void bindIndexes(Table table, jakarta.persistence.Index[] indexes) {
221233
ordering,
222234
unique,
223235
true,
236+
type,
237+
using,
224238
options,
225239
selectables( table, name, columnExpressions )
226240
);
@@ -240,6 +254,8 @@ void bindUniqueConstraints(Table table, UniqueConstraint[] constraints) {
240254
null,
241255
true,
242256
false,
257+
null,
258+
null,
243259
options,
244260
columns( table, name, columnNames )
245261
);

‎hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class TableBinder {
7272
private boolean isJPA2ElementCollection;
7373
private UniqueConstraint[] uniqueConstraints;
7474
private Index[] indexes;
75+
private String type;
7576
private String options;
7677

7778
public void setBuildingContext(MetadataBuildingContext buildingContext) {
@@ -110,6 +111,10 @@ public void setOptions(String options) {
110111
this.options = options;
111112
}
112113

114+
public void setType(String type) {
115+
this.type = type;
116+
}
117+
113118
public void setJPA2ElementCollection(boolean isJPA2ElementCollection) {
114119
this.isJPA2ElementCollection = isJPA2ElementCollection;
115120
}
@@ -293,6 +298,7 @@ public Identifier toPhysicalName(Identifier logicalName, MetadataBuildingContext
293298
isAbstract,
294299
uniqueConstraints,
295300
indexes,
301+
type,
296302
options,
297303
buildingContext,
298304
null,
@@ -448,6 +454,7 @@ public static Table buildAndFillTable(
448454
uniqueConstraints,
449455
null,
450456
null,
457+
null,
451458
buildingContext,
452459
null,
453460
null
@@ -471,6 +478,7 @@ public static Table buildAndFillTable(
471478
uniqueConstraints,
472479
null,
473480
null,
481+
null,
474482
buildingContext,
475483
subselect,
476484
denormalizedSuperTableXref
@@ -484,6 +492,7 @@ private static Table buildAndFillTable(
484492
boolean isAbstract,
485493
UniqueConstraint[] uniqueConstraints,
486494
Index[] indexes,
495+
String type,
487496
String options,
488497
MetadataBuildingContext buildingContext,
489498
String subselect,
@@ -509,6 +518,8 @@ private static Table buildAndFillTable(
509518
new IndexBinder( buildingContext ).bindIndexes( table, indexes );
510519
}
511520

521+
addTableType( table, type );
522+
512523
if ( options != null ) {
513524
table.setOptions( options );
514525
}
@@ -919,6 +930,12 @@ static void addTableComment(Table table, String comment) {
919930
}
920931
}
921932

933+
static void addTableType(Table table, String type) {
934+
if ( isNotBlank( type ) ) {
935+
table.setType( type );
936+
}
937+
}
938+
922939
static void addTableOptions(Table table, String options) {
923940
if ( isNotBlank( options ) ) {
924941
table.setOptions( options );

‎hibernate-core/src/main/java/org/hibernate/mapping/Index.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class Index implements Exportable, Serializable {
3131
private Identifier name;
3232
private Table table;
3333
private boolean unique;
34+
private String type = "";
35+
private String using = "";
3436
private String options = "";
3537
private final java.util.List<Selectable> selectables = new ArrayList<>();
3638
private final java.util.Map<Selectable, String> selectableOrderMap = new HashMap<>();
@@ -51,6 +53,22 @@ public boolean isUnique() {
5153
return unique;
5254
}
5355

56+
public String getType() {
57+
return type;
58+
}
59+
60+
public void setType(String type) {
61+
this.type = type;
62+
}
63+
64+
public String getUsing() {
65+
return using;
66+
}
67+
68+
public void setUsing(String using) {
69+
this.using = using;
70+
}
71+
5472
public String getOptions() {
5573
return options;
5674
}

‎hibernate-core/src/main/java/org/hibernate/mapping/Table.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class Table implements Serializable, ContributableDatabaseObject {
6969
private boolean hasDenormalizedTables;
7070
private String comment;
7171
private String viewQuery;
72+
private String type;
7273
private String options;
7374
private String extraDeclarations;
7475

@@ -855,6 +856,14 @@ public void setOptions(String options) {
855856
this.options = options;
856857
}
857858

859+
public String getType() {
860+
return type;
861+
}
862+
863+
public void setType(String type) {
864+
this.type = type;
865+
}
866+
858867
public String getExtraDeclarations() {
859868
return extraDeclarations;
860869
}

‎hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardIndexExporter.java‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.hibernate.mapping.Index;
1313
import org.hibernate.tool.schema.spi.Exporter;
1414

15+
import static org.hibernate.internal.util.StringHelper.isBlank;
1516
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
1617
import static org.hibernate.internal.util.StringHelper.qualify;
1718

@@ -35,12 +36,16 @@ protected Dialect getDialect() {
3536
@Override
3637
public String[] getSqlCreateStrings(Index index, Metadata metadata, SqlStringGenerationContext context) {
3738
final var createIndex = new StringBuilder()
38-
.append( dialect.getCreateIndexString( index.isUnique() ) )
39+
.append( createIndexString( index ) )
3940
.append( " " )
4041
.append( indexName( index, context, metadata ) )
4142
.append( " on " )
42-
.append( context.format( index.getTable().getQualifiedTableName() ) )
43-
.append( " (" );
43+
.append( context.format( index.getTable().getQualifiedTableName() ) );
44+
if ( isNotEmpty( index.getUsing() ) ) {
45+
createIndex.append( " using " )
46+
.append( index.getUsing() );
47+
}
48+
createIndex.append( " (" );
4449
appendColumnList( index, createIndex );
4550
createIndex.append( ")" );
4651
if ( isNotEmpty( index.getOptions() ) ) {
@@ -49,6 +54,15 @@ public String[] getSqlCreateStrings(Index index, Metadata metadata, SqlStringGen
4954
return new String[] { createIndex.toString() };
5055
}
5156

57+
private String createIndexString(Index index) {
58+
final String createIndexString = dialect.getCreateIndexString( index.isUnique() );
59+
final String type = index.getType();
60+
return isBlank( type )
61+
? createIndexString
62+
: createIndexString.replaceFirst( " (?i:index)",
63+
' ' + type + " index" );
64+
}
65+
5266
private String indexName(Index index, SqlStringGenerationContext context, Metadata metadata) {
5367
if ( dialect.qualifyIndexName() ) {
5468
final var qualifiedTableName = index.getTable().getQualifiedTableName();

‎hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static java.util.Comparator.comparing;
3232
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
3333
import static org.hibernate.internal.util.StringHelper.EMPTY_STRINGS;
34+
import static org.hibernate.internal.util.StringHelper.isBlank;
3435
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
3536
import static org.hibernate.tool.schema.internal.ColumnDefinitions.appendColumn;
3637

@@ -87,7 +88,7 @@ public String[] getSqlCreateStrings(
8788
else {
8889
final var extra = new StringBuilder();
8990

90-
createTable.append( tableCreateString( table.hasPrimaryKey() ) )
91+
createTable.append( tableCreateString( table ) )
9192
.append( ' ' )
9293
.append( formattedTableName )
9394
.append( " (" );
@@ -386,9 +387,16 @@ else if ( checkConstraints.size() > 1 ) {
386387
}
387388
}
388389

389-
protected String tableCreateString(boolean hasPrimaryKey) {
390-
return hasPrimaryKey ? dialect.getCreateTableString() : dialect.getCreateMultisetTableString();
391-
390+
private String tableCreateString(Table table) {
391+
final String createTableString =
392+
table.hasPrimaryKey()
393+
? dialect.getCreateTableString()
394+
: dialect.getCreateMultisetTableString();
395+
final String type = table.getType();
396+
return isBlank( type )
397+
? createTableString
398+
: createTableString.replaceFirst( " (?i:table)",
399+
' ' + type + " table" );
392400
}
393401

394402
protected String primaryKeyString(PrimaryKey key) {

0 commit comments

Comments
 (0)