44
44
import org .hibernate .exception .LockTimeoutException ;
45
45
import org .hibernate .exception .SQLGrammarException ;
46
46
import org .hibernate .exception .spi .SQLExceptionConversionDelegate ;
47
+ import org .hibernate .exception .spi .TemplatedViolatedConstraintNameExtractor ;
48
+ import org .hibernate .exception .spi .ViolatedConstraintNameExtractor ;
47
49
import org .hibernate .mapping .Table ;
48
50
import org .hibernate .metamodel .mapping .EntityMappingType ;
49
51
import org .hibernate .metamodel .spi .RuntimeModelCreationContext ;
126
128
import java .util .regex .Pattern ;
127
129
128
130
import static org .hibernate .dialect .HANAServerConfiguration .MAX_LOB_PREFETCH_SIZE_DEFAULT_VALUE ;
131
+ import static org .hibernate .exception .spi .TemplatedViolatedConstraintNameExtractor .extractUsingTemplate ;
129
132
import static org .hibernate .internal .util .JdbcExceptionHelper .extractErrorCode ;
130
133
import static org .hibernate .query .sqm .produce .function .FunctionParameterType .ANY ;
131
134
import static org .hibernate .type .SqlTypes .BINARY ;
@@ -618,8 +621,6 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
618
621
new ConstraintViolationException ( message , sqlException , sql , ConstraintKind .NOT_NULL ,
619
622
getViolatedConstraintNameExtractor ().extractConstraintName ( sqlException ) );
620
623
case 461 , 462 ->
621
- // 257 - Cannot insert NULL or update to NULL
622
- // 301 - Unique constraint violated
623
624
// 461 - foreign key constraint violation
624
625
// 462 - failed on update or delete by foreign key constraint violation
625
626
new ConstraintViolationException ( message , sqlException , sql , ConstraintKind .FOREIGN_KEY ,
@@ -633,6 +634,17 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
633
634
};
634
635
}
635
636
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
+
636
648
@ Override
637
649
public RowLockStrategy getWriteRowLockStrategy () {
638
650
return RowLockStrategy .COLUMN ;
0 commit comments