Skip to content

Commit 9069d72

Browse files
committed
HHH-19300 constraint name extraction for HANA
1 parent f16ac83 commit 9069d72

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.hibernate.exception.LockTimeoutException;
4545
import org.hibernate.exception.SQLGrammarException;
4646
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
47+
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
48+
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
4749
import org.hibernate.mapping.Table;
4850
import org.hibernate.metamodel.mapping.EntityMappingType;
4951
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
@@ -126,6 +128,7 @@
126128
import java.util.regex.Pattern;
127129

128130
import static org.hibernate.dialect.HANAServerConfiguration.MAX_LOB_PREFETCH_SIZE_DEFAULT_VALUE;
131+
import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate;
129132
import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode;
130133
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ANY;
131134
import static org.hibernate.type.SqlTypes.BINARY;
@@ -618,8 +621,6 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
618621
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.NOT_NULL,
619622
getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) );
620623
case 461, 462 ->
621-
// 257 - Cannot insert NULL or update to NULL
622-
// 301 - Unique constraint violated
623624
// 461 - foreign key constraint violation
624625
// 462 - failed on update or delete by foreign key constraint violation
625626
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.FOREIGN_KEY,
@@ -633,6 +634,17 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
633634
};
634635
}
635636

637+
@Override
638+
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
639+
return new TemplatedViolatedConstraintNameExtractor( sqlException ->
640+
switch ( extractErrorCode( sqlException ) ) {
641+
case 301 -> extractUsingTemplate(" Index(", ") ", sqlException.getMessage() );
642+
case 287 -> extractUsingTemplate(" NULL: ", ": ", sqlException.getMessage() );
643+
case 677 -> extractUsingTemplate(" violation: ", ": ", sqlException.getMessage() );
644+
default -> null;
645+
} );
646+
}
647+
636648
@Override
637649
public RowLockStrategy getWriteRowLockStrategy() {
638650
return RowLockStrategy.COLUMN;

hibernate-core/src/test/java/org/hibernate/orm/test/constraint/ConstraintInterpretationTest2.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import jakarta.persistence.UniqueConstraint;
1616
import org.hibernate.dialect.DB2Dialect;
1717
import org.hibernate.dialect.H2Dialect;
18+
import org.hibernate.dialect.HANADialect;
1819
import org.hibernate.dialect.HSQLDialect;
1920
import org.hibernate.dialect.MySQLDialect;
2021
import org.hibernate.dialect.OracleDialect;
@@ -41,6 +42,7 @@
4142
@RequiresDialect( SybaseASEDialect.class )
4243
@RequiresDialect( OracleDialect.class )
4344
@RequiresDialect( DB2Dialect.class )
45+
@RequiresDialect( HANADialect.class )
4446
public class ConstraintInterpretationTest2 {
4547
@Test void testNotNullPrimaryKey(EntityManagerFactoryScope scope) {
4648
scope.inTransaction( em -> {
@@ -117,7 +119,9 @@ public class ConstraintInterpretationTest2 {
117119
}
118120
catch (ConstraintViolationException cve) {
119121
assertEquals( ConstraintViolationException.ConstraintKind.FOREIGN_KEY, cve.getKind() );
120-
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id2to1fk" ) );
122+
if ( !(scope.getDialect() instanceof HANADialect) ) {
123+
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id2to1fk" ) );
124+
}
121125
}
122126
} );
123127
}

0 commit comments

Comments
 (0)