Skip to content

Commit 36724b9

Browse files
committed
HHH-19608 automatically add @PartitionKey to the primary key
on databases where this is required also enable support for table partitioning on MySQL and Maria
1 parent d4e8929 commit 36724b9

File tree

8 files changed

+55
-1
lines changed

8 files changed

+55
-1
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,6 +4622,16 @@ public boolean supportsPartitionBy() {
46224622
return false;
46234623
}
46244624

4625+
/**
4626+
* Does this dialect require that the columns listed in
4627+
* {@code partition by} also occur in the primary key?
4628+
*
4629+
* @since 7.1
4630+
*/
4631+
public boolean addPartitionKeyToPrimaryKey() {
4632+
return false;
4633+
}
4634+
46254635
/**
46264636
* Override {@link DatabaseMetaData#supportsNamedParameters()}.
46274637
*

hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,16 @@ public String getSelectGUIDString() {
10991099
return "select uuid()";
11001100
}
11011101

1102+
@Override
1103+
public boolean supportsPartitionBy() {
1104+
return true;
1105+
}
1106+
1107+
@Override
1108+
public boolean addPartitionKeyToPrimaryKey() {
1109+
return true;
1110+
}
1111+
11021112
@Override
11031113
public boolean supportsCommentOn() {
11041114
return true;

hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,11 @@ public boolean supportsPartitionBy() {
821821
return true;
822822
}
823823

824+
@Override
825+
public boolean addPartitionKeyToPrimaryKey() {
826+
return true;
827+
}
828+
824829
@Override
825830
public boolean supportsNonQueryWithCTE() {
826831
return true;

hibernate-core/src/main/java/org/hibernate/id/enhanced/ExportableColumnHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public boolean isColumnInsertable(int index) {
143143
public boolean isColumnUpdateable(int index) {
144144
return true;
145145
}
146+
147+
@Override
148+
public boolean isPartitionKey() {
149+
return false;
150+
}
146151
} );
147152
return column;
148153
}

hibernate-core/src/main/java/org/hibernate/mapping/Collection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,4 +897,9 @@ public Supplier<? extends Expectation> getDeleteAllExpectation() {
897897
public void setDeleteAllExpectation(Supplier<? extends Expectation> deleteAllExpectation) {
898898
this.deleteAllExpectation = deleteAllExpectation;
899899
}
900+
901+
@Override
902+
public boolean isPartitionKey() {
903+
return false;
904+
}
900905
}

hibernate-core/src/main/java/org/hibernate/mapping/OneToMany.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,9 @@ public boolean isColumnUpdateable(int index) {
234234
public String toString() {
235235
return getClass().getSimpleName();
236236
}
237+
238+
@Override
239+
public boolean isPartitionKey() {
240+
return false;
241+
}
237242
}

hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,22 @@ public void createPrimaryKey() {
427427
final PrimaryKey pk = new PrimaryKey( table );
428428
pk.setName( PK_ALIAS.toAliasString( table.getName() ) );
429429
pk.addColumns( getKey() );
430-
430+
if ( addPartitionKeyToPrimaryKey() ) {
431+
for ( Property property : getProperties() ) {
432+
if ( property.getValue().isPartitionKey() ) {
433+
pk.addColumns( property.getValue() );
434+
}
435+
}
436+
}
431437
table.setPrimaryKey( pk );
432438
}
433439

440+
private boolean addPartitionKeyToPrimaryKey() {
441+
return metadataBuildingContext.getMetadataCollector()
442+
.getDatabase().getDialect()
443+
.addPartitionKeyToPrimaryKey();
444+
}
445+
434446
public abstract String getWhere();
435447

436448
public int getBatchSize() {

hibernate-core/src/main/java/org/hibernate/mapping/Value.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ private Type getIdType(EntityType entityType) {
123123

124124
boolean isAlternateUniqueKey();
125125

126+
boolean isPartitionKey();
127+
126128
boolean isNullable();
127129

128130
void createForeignKey();

0 commit comments

Comments
 (0)