Skip to content

Commit 1f5b18d

Browse files
committed
HHH-19300 constraint interpretation improvements for Db2
1 parent 25ee50d commit 1f5b18d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,15 @@ public void appendBinaryLiteral(SqlAppender appender, byte[] bytes) {
11231123
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
11241124
return new TemplatedViolatedConstraintNameExtractor(
11251125
sqle -> switch ( extractErrorCode( sqle ) ) {
1126-
case -803 -> extractUsingTemplate( "SQLERRMC=1;", ",", sqle.getMessage() );
1126+
case -803 -> {
1127+
// Unique constraint
1128+
final String constraintWithKind = extractUsingTemplate( "SQLERRMC=", ",", sqle.getMessage() );
1129+
// strip off "1;" for PK, or "2;" for other UK
1130+
yield constraintWithKind == null ? null : constraintWithKind.substring(2);
1131+
}
1132+
case -543, -545, -530,-531 ->
1133+
// Foreign key or check constraint
1134+
extractUsingTemplate( "SQLERRMC=", ",", sqle.getMessage() );
11271135
default -> null;
11281136
}
11291137
);

hibernate-core/src/test/java/org/hibernate/orm/test/constraint/ConstraintInterpretationTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import jakarta.persistence.ManyToOne;
1414
import jakarta.persistence.Table;
1515
import jakarta.persistence.UniqueConstraint;
16+
import org.hibernate.dialect.DB2Dialect;
1617
import org.hibernate.dialect.H2Dialect;
1718
import org.hibernate.dialect.HSQLDialect;
1819
import org.hibernate.dialect.MariaDBDialect;
@@ -42,6 +43,7 @@
4243
@RequiresDialect( SQLServerDialect.class )
4344
@RequiresDialect( SybaseASEDialect.class )
4445
@RequiresDialect( OracleDialect.class )
46+
@RequiresDialect( DB2Dialect.class )
4547
@SkipForDialect(dialectClass = MariaDBDialect.class) // Maria doesn't allow named column-level check constraints
4648
public class ConstraintInterpretationTest {
4749
@Test void testNotNullPrimaryKey(EntityManagerFactoryScope scope) {
@@ -52,7 +54,9 @@ public class ConstraintInterpretationTest {
5254
}
5355
catch (ConstraintViolationException cve) {
5456
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
55-
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id" ) );
57+
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
58+
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id" ) );
59+
}
5660
}
5761
} );
5862
}
@@ -76,7 +80,9 @@ public class ConstraintInterpretationTest {
7680
}
7781
catch (ConstraintViolationException cve) {
7882
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
79-
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "name" ) );
83+
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
84+
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "name" ) );
85+
}
8086
}
8187
} );
8288
}
@@ -89,7 +95,9 @@ public class ConstraintInterpretationTest {
8995
}
9096
catch (ConstraintViolationException cve) {
9197
assertEquals( ConstraintViolationException.ConstraintKind.UNIQUE, cve.getKind() );
92-
assertTrue( cve.getConstraintName().toLowerCase().contains( "ssnuk" ) );
98+
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
99+
assertTrue( cve.getConstraintName().toLowerCase().contains( "ssnuk" ) );
100+
}
93101
}
94102
} );
95103
}

hibernate-core/src/test/java/org/hibernate/orm/test/constraint/ConstraintInterpretationTest2.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import jakarta.persistence.ManyToOne;
1414
import jakarta.persistence.Table;
1515
import jakarta.persistence.UniqueConstraint;
16+
import org.hibernate.dialect.DB2Dialect;
1617
import org.hibernate.dialect.H2Dialect;
1718
import org.hibernate.dialect.HSQLDialect;
1819
import org.hibernate.dialect.MySQLDialect;
@@ -39,6 +40,7 @@
3940
@RequiresDialect( SQLServerDialect.class )
4041
@RequiresDialect( SybaseASEDialect.class )
4142
@RequiresDialect( OracleDialect.class )
43+
@RequiresDialect( DB2Dialect.class )
4244
public class ConstraintInterpretationTest2 {
4345
@Test void testNotNullPrimaryKey(EntityManagerFactoryScope scope) {
4446
scope.inTransaction( em -> {
@@ -48,7 +50,9 @@ public class ConstraintInterpretationTest2 {
4850
}
4951
catch (ConstraintViolationException cve) {
5052
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
51-
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id" ) );
53+
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
54+
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id" ) );
55+
}
5256
}
5357
} );
5458
}
@@ -72,7 +76,9 @@ public class ConstraintInterpretationTest2 {
7276
}
7377
catch (ConstraintViolationException cve) {
7478
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
75-
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "name" ) );
79+
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
80+
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "name" ) );
81+
}
7682
}
7783
} );
7884
}
@@ -85,7 +91,9 @@ public class ConstraintInterpretationTest2 {
8591
}
8692
catch (ConstraintViolationException cve) {
8793
assertEquals( ConstraintViolationException.ConstraintKind.UNIQUE, cve.getKind() );
88-
assertTrue( cve.getConstraintName().toLowerCase().contains( "ssnuk" ) );
94+
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
95+
assertTrue( cve.getConstraintName().toLowerCase().contains( "ssnuk" ) );
96+
}
8997
}
9098
} );
9199
}

0 commit comments

Comments
 (0)