Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.hibernate.exception.LockTimeoutException;
import org.hibernate.exception.SQLGrammarException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.mapping.Table;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
Expand Down Expand Up @@ -126,6 +128,7 @@
import java.util.regex.Pattern;

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

@Override
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
return new TemplatedViolatedConstraintNameExtractor( sqlException ->
switch ( extractErrorCode( sqlException ) ) {
case 301 -> extractUsingTemplate(" Index(", ") ", sqlException.getMessage() );
case 287 -> extractUsingTemplate(" NULL: ", ": ", sqlException.getMessage() );
case 677 -> extractUsingTemplate(" violation: ", ": ", sqlException.getMessage() );
default -> null;
} );
}

@Override
public RowLockStrategy getWriteRowLockStrategy() {
return RowLockStrategy.COLUMN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import jakarta.persistence.UniqueConstraint;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.HANADialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.OracleDialect;
Expand All @@ -41,6 +42,7 @@
@RequiresDialect( SybaseASEDialect.class )
@RequiresDialect( OracleDialect.class )
@RequiresDialect( DB2Dialect.class )
@RequiresDialect( HANADialect.class )
public class ConstraintInterpretationTest2 {
@Test void testNotNullPrimaryKey(EntityManagerFactoryScope scope) {
scope.inTransaction( em -> {
Expand Down Expand Up @@ -117,7 +119,9 @@ public class ConstraintInterpretationTest2 {
}
catch (ConstraintViolationException cve) {
assertEquals( ConstraintViolationException.ConstraintKind.FOREIGN_KEY, cve.getKind() );
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id2to1fk" ) );
if ( !(scope.getDialect() instanceof HANADialect) ) {
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id2to1fk" ) );
}
}
} );
}
Expand Down