4444import org .hibernate .exception .LockTimeoutException ;
4545import org .hibernate .exception .SQLGrammarException ;
4646import org .hibernate .exception .spi .SQLExceptionConversionDelegate ;
47+ import org .hibernate .exception .spi .TemplatedViolatedConstraintNameExtractor ;
48+ import org .hibernate .exception .spi .ViolatedConstraintNameExtractor ;
4749import org .hibernate .mapping .Table ;
4850import org .hibernate .metamodel .mapping .EntityMappingType ;
4951import org .hibernate .metamodel .spi .RuntimeModelCreationContext ;
126128import java .util .regex .Pattern ;
127129
128130import static org .hibernate .dialect .HANAServerConfiguration .MAX_LOB_PREFETCH_SIZE_DEFAULT_VALUE ;
131+ import static org .hibernate .exception .spi .TemplatedViolatedConstraintNameExtractor .extractUsingTemplate ;
129132import static org .hibernate .internal .util .JdbcExceptionHelper .extractErrorCode ;
130133import static org .hibernate .query .sqm .produce .function .FunctionParameterType .ANY ;
131134import 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 ;
0 commit comments