11
11
import org .hibernate .QueryTimeoutException ;
12
12
import org .hibernate .exception .AuthException ;
13
13
import org .hibernate .exception .ConstraintViolationException ;
14
+ import org .hibernate .exception .ConstraintViolationException .ConstraintKind ;
14
15
import org .hibernate .exception .DataException ;
15
16
import org .hibernate .exception .JDBCConnectionException ;
16
17
import org .hibernate .exception .LockAcquisitionException ;
@@ -78,10 +79,16 @@ public SQLStateConversionDelegate(ConversionContext conversionContext) {
78
79
"23" , // "integrity constraint violation"
79
80
"27" , // "triggered data change violation"
80
81
"44" : // "with check option violation"
81
- final String constraintName = getConversionContext ()
82
- .getViolatedConstraintNameExtractor ()
83
- .extractConstraintName ( sqlException );
84
- return new ConstraintViolationException ( message , sqlException , sql , constraintName );
82
+ final String constraintName =
83
+ getConversionContext ().getViolatedConstraintNameExtractor ()
84
+ .extractConstraintName ( sqlException );
85
+ if ( sqlState .length () > 5 ) {
86
+ final ConstraintKind constraintKind = constraintKind ( sqlState .substring ( 0 , 5 ) );
87
+ return new ConstraintViolationException ( message , sqlException , sql , constraintKind , constraintName );
88
+ }
89
+ else {
90
+ return new ConstraintViolationException ( message , sqlException , sql , constraintName );
91
+ }
85
92
case
86
93
"08" : // "connection exception"
87
94
return new JDBCConnectionException ( message , sqlException , sql );
@@ -96,4 +103,17 @@ public SQLStateConversionDelegate(ConversionContext conversionContext) {
96
103
}
97
104
return null ;
98
105
}
106
+
107
+ private static ConstraintKind constraintKind (String trimmedState ) {
108
+ return switch ( trimmedState ) {
109
+ case "23502" -> ConstraintKind .NOT_NULL ;
110
+ case "23505" -> ConstraintKind .UNIQUE ;
111
+ case "23503" -> ConstraintKind .FOREIGN_KEY ;
112
+ // 23510-3 indicate CHECK on Db2,
113
+ // 23514 indicates CHECK on Postgres,
114
+ // 23513-4 indicate CHECK on h2
115
+ case "23510" , "23511" , "23512" , "23513" , "23514" -> ConstraintKind .CHECK ;
116
+ default -> ConstraintKind .OTHER ;
117
+ };
118
+ }
99
119
}
0 commit comments