Skip to content

Commit e49d173

Browse files
committed
HHH-19300 constraint interpretation improvements for HANA
1 parent 1f5b18d commit e49d173

File tree

1 file changed

+45
-54
lines changed

1 file changed

+45
-54
lines changed

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

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
4040
import org.hibernate.engine.spi.SessionFactoryImplementor;
4141
import org.hibernate.exception.ConstraintViolationException;
42+
import org.hibernate.exception.ConstraintViolationException.ConstraintKind;
4243
import org.hibernate.exception.LockAcquisitionException;
4344
import org.hibernate.exception.LockTimeoutException;
4445
import org.hibernate.exception.SQLGrammarException;
4546
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
46-
import org.hibernate.internal.util.JdbcExceptionHelper;
4747
import org.hibernate.mapping.Table;
4848
import org.hibernate.metamodel.mapping.EntityMappingType;
4949
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
@@ -126,6 +126,7 @@
126126
import java.util.regex.Pattern;
127127

128128
import static org.hibernate.dialect.HANAServerConfiguration.MAX_LOB_PREFETCH_SIZE_DEFAULT_VALUE;
129+
import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode;
129130
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ANY;
130131
import static org.hibernate.type.SqlTypes.BINARY;
131132
import static org.hibernate.type.SqlTypes.BOOLEAN;
@@ -587,59 +588,49 @@ public String extractPattern(TemporalUnit unit) {
587588

588589
@Override
589590
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
590-
return (sqlException, message, sql) -> {
591-
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
592-
593-
if ( errorCode == 131 ) {
594-
// 131 - Transaction rolled back by lock wait timeout
595-
return new LockTimeoutException( message, sqlException, sql );
596-
}
597-
598-
if ( errorCode == 146 ) {
599-
// 146 - Resource busy and acquire with NOWAIT specified
600-
return new LockTimeoutException( message, sqlException, sql );
601-
}
602-
603-
if ( errorCode == 132 ) {
604-
// 132 - Transaction rolled back due to unavailable resource
605-
return new LockAcquisitionException( message, sqlException, sql );
606-
}
607-
608-
if ( errorCode == 133 ) {
609-
// 133 - Transaction rolled back by detected deadlock
610-
return new LockAcquisitionException( message, sqlException, sql );
611-
}
612-
613-
// 259 - Invalid table name
614-
// 260 - Invalid column name
615-
// 261 - Invalid index name
616-
// 262 - Invalid query name
617-
// 263 - Invalid alias name
618-
if ( errorCode == 257 || ( errorCode >= 259 && errorCode <= 263 ) ) {
619-
return new SQLGrammarException( message, sqlException, sql );
620-
}
621-
622-
// 257 - Cannot insert NULL or update to NULL
623-
// 301 - Unique constraint violated
624-
// 461 - foreign key constraint violation
625-
// 462 - failed on update or delete by foreign key constraint violation
626-
if ( errorCode == 287 || errorCode == 301 || errorCode == 461 || errorCode == 462 ) {
627-
final String constraintName = getViolatedConstraintNameExtractor()
628-
.extractConstraintName( sqlException );
629-
630-
return new ConstraintViolationException(
631-
message,
632-
sqlException,
633-
sql,
634-
errorCode == 301
635-
? ConstraintViolationException.ConstraintKind.UNIQUE
636-
: ConstraintViolationException.ConstraintKind.OTHER,
637-
constraintName
638-
);
639-
}
640-
641-
return null;
642-
};
591+
return (sqlException, message, sql) ->
592+
switch ( extractErrorCode( sqlException ) ) {
593+
case 131 ->
594+
// 131 - Transaction rolled back by lock wait timeout
595+
new LockTimeoutException( message, sqlException, sql );
596+
case 146 ->
597+
// 146 - Resource busy and acquire with NOWAIT specified
598+
new LockTimeoutException( message, sqlException, sql );
599+
case 132 ->
600+
// 132 - Transaction rolled back due to unavailable resource
601+
new LockAcquisitionException( message, sqlException, sql );
602+
case 133 ->
603+
// 133 - Transaction rolled back by detected deadlock
604+
new LockAcquisitionException( message, sqlException, sql );
605+
case 257, 259, 260, 261, 262, 263 ->
606+
// 259 - Invalid table name
607+
// 260 - Invalid column name
608+
// 261 - Invalid index name
609+
// 262 - Invalid query name
610+
// 263 - Invalid alias name
611+
new SQLGrammarException( message, sqlException, sql );
612+
case 301 ->
613+
// 301 - Unique constraint violated
614+
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.UNIQUE,
615+
getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) );
616+
case 287 ->
617+
// 287 - Cannot insert NULL or update to NULL
618+
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.NOT_NULL,
619+
getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) );
620+
case 461, 462 ->
621+
// 257 - Cannot insert NULL or update to NULL
622+
// 301 - Unique constraint violated
623+
// 461 - foreign key constraint violation
624+
// 462 - failed on update or delete by foreign key constraint violation
625+
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.FOREIGN_KEY,
626+
getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) );
627+
case 677 ->
628+
// 677 - Check constraint violation
629+
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.CHECK,
630+
getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) );
631+
632+
default -> null;
633+
};
643634
}
644635

645636
@Override

0 commit comments

Comments
 (0)