|
81 | 81 |
|
82 | 82 | import jakarta.persistence.TemporalType;
|
83 | 83 |
|
| 84 | +import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate; |
84 | 85 | import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode;
|
85 | 86 | import static org.hibernate.query.common.TemporalUnit.SECOND;
|
86 | 87 | import static org.hibernate.type.SqlTypes.BIGINT;
|
@@ -783,24 +784,21 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
|
783 | 784 | }
|
784 | 785 |
|
785 | 786 | private static final ViolatedConstraintNameExtractor EXTRACTOR =
|
786 |
| - new TemplatedViolatedConstraintNameExtractor( sqle -> { |
787 |
| - // 23000: Check constraint violation: {0} |
788 |
| - // 23001: Unique index or primary key violation: {0} |
789 |
| - if ( sqle.getSQLState().startsWith( "23" ) ) { |
790 |
| - final String message = sqle.getMessage(); |
791 |
| - final int i = message.indexOf( "violation: " ); |
792 |
| - if ( i > 0 ) { |
793 |
| - String constraintDescription = |
794 |
| - message.substring( i + "violation: ".length() ) |
795 |
| - .replace( "\"", "" ); |
796 |
| - if ( sqle.getSQLState().equals( "23506" ) ) { |
797 |
| - constraintDescription = constraintDescription.substring( 1, constraintDescription.indexOf( ':' ) ); |
798 |
| - } |
799 |
| - final int j = constraintDescription.indexOf(" ON "); |
800 |
| - return j>0 ? constraintDescription.substring(0, j) : constraintDescription; |
801 |
| - } |
| 787 | + new TemplatedViolatedConstraintNameExtractor( sqle -> switch ( extractErrorCode( sqle ) ) { |
| 788 | + case 23505 -> { |
| 789 | + // Unique index or primary key violation |
| 790 | + final String constraint = |
| 791 | + extractUsingTemplate( "violation: \"", "\"", sqle.getMessage() ); |
| 792 | + final int onIndex = constraint == null ? -1 : constraint.indexOf( " ON " ); |
| 793 | + yield onIndex > 0 ? constraint.substring( 0, onIndex ) : constraint; |
802 | 794 | }
|
803 |
| - return null; |
| 795 | + case 23502 -> |
| 796 | + // NULL not allowed for column |
| 797 | + extractUsingTemplate( "column \"", "\"", sqle.getMessage() ); |
| 798 | + case 23503, 23506, 23513, 23514 -> |
| 799 | + // Referential integrity or check constraint violation |
| 800 | + extractUsingTemplate( "constraint violation: \"", ":", sqle.getMessage() ); |
| 801 | + default -> null; |
804 | 802 | } );
|
805 | 803 |
|
806 | 804 | @Override
|
|
0 commit comments