Skip to content

Commit 47db687

Browse files
committed
Skip some constraint violation name check for HANA and CockroachDB and improve Informix constraint reporting
1 parent 4d3aee5 commit 47db687

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/InformixDialect.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.hibernate.exception.ConstraintViolationException;
4040
import org.hibernate.exception.LockAcquisitionException;
4141
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
42+
import org.hibernate.mapping.CheckConstraint;
4243
import org.hibernate.query.sqm.CastType;
4344
import org.hibernate.query.sqm.IntervalType;
4445
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
@@ -112,6 +113,7 @@
112113

113114
import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate;
114115
import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode;
116+
import static org.hibernate.internal.util.StringHelper.isBlank;
115117
import static org.hibernate.query.common.TemporalUnit.DAY;
116118
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING;
117119
import static org.hibernate.query.sqm.produce.function.StandardFunctionArgumentTypeResolvers.impliedOrInvariant;
@@ -605,11 +607,22 @@ public boolean supportsIfExistsBeforeConstraintName() {
605607
}
606608

607609
@Override
608-
public boolean supportsTableCheck() {
609-
// multi-column check constraints are created using 'alter table'
610+
public boolean supportsNamedColumnCheck() {
611+
// It seems the constraint name is ignored on column level
610612
return false;
611613
}
612614

615+
@Override
616+
public String getCheckConstraintString(CheckConstraint checkConstraint) {
617+
final String constraintName = checkConstraint.getName();
618+
final String constraint = " check (" + checkConstraint.getConstraint() + ")";
619+
final String constraintWithName =
620+
isBlank( constraintName )
621+
? constraint
622+
: constraint + " constraint " + constraintName;
623+
return appendCheckConstraintOptions( checkConstraint, constraintWithName );
624+
}
625+
613626
@Override
614627
public String getCascadeConstraintsString() {
615628
return getVersion().isSameOrAfter( 12, 10 )

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import jakarta.persistence.Table;
1515
import jakarta.persistence.UniqueConstraint;
1616
import org.hibernate.community.dialect.InformixDialect;
17+
import org.hibernate.dialect.CockroachDialect;
1718
import org.hibernate.dialect.DB2Dialect;
19+
import org.hibernate.dialect.HANADialect;
1820
import org.hibernate.exception.ConstraintViolationException;
1921
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
2022
import org.hibernate.testing.orm.junit.Jpa;
@@ -36,6 +38,7 @@ public class ConstraintInterpretationTest {
3638
}
3739
catch (ConstraintViolationException cve) {
3840
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
41+
// DB2 and Informix error messages don't contain the primary key constraint name
3942
if ( !(scope.getDialect() instanceof DB2Dialect) && !(scope.getDialect() instanceof InformixDialect) ) {
4043
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id" ) );
4144
}
@@ -62,6 +65,7 @@ public class ConstraintInterpretationTest {
6265
}
6366
catch (ConstraintViolationException cve) {
6467
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
68+
// DB2 error message doesn't contain constraint or column name
6569
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
6670
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "name" ) );
6771
}
@@ -77,6 +81,7 @@ public class ConstraintInterpretationTest {
7781
}
7882
catch (ConstraintViolationException cve) {
7983
assertEquals( ConstraintViolationException.ConstraintKind.UNIQUE, cve.getKind() );
84+
// DB2 error message doesn't contain unique constraint name
8085
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
8186
assertTrue( cve.getConstraintName().toLowerCase().contains( "ssnuk" ) );
8287
}
@@ -91,7 +96,8 @@ public class ConstraintInterpretationTest {
9196
}
9297
catch (ConstraintViolationException cve) {
9398
assertEquals( ConstraintViolationException.ConstraintKind.CHECK, cve.getKind() );
94-
if ( !(scope.getDialect() instanceof InformixDialect) ) {
99+
// CockroachDB error messages don't contain the check constraint name
100+
if ( !(scope.getDialect() instanceof CockroachDialect) ) {
95101
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "namecheck" ) );
96102
}
97103
}
@@ -105,7 +111,10 @@ public class ConstraintInterpretationTest {
105111
}
106112
catch (ConstraintViolationException cve) {
107113
assertEquals( ConstraintViolationException.ConstraintKind.FOREIGN_KEY, cve.getKind() );
108-
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id2to1fk" ) );
114+
// HANA error messages don't contain the foreign key constraint name
115+
if ( !(scope.getDialect() instanceof HANADialect) ) {
116+
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id2to1fk" ) );
117+
}
109118
}
110119
} );
111120
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
import jakarta.persistence.Table;
1515
import jakarta.persistence.UniqueConstraint;
1616
import org.hibernate.community.dialect.InformixDialect;
17+
import org.hibernate.dialect.CockroachDialect;
1718
import org.hibernate.dialect.DB2Dialect;
1819
import org.hibernate.dialect.HANADialect;
1920
import org.hibernate.exception.ConstraintViolationException;
2021
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
2122
import org.hibernate.testing.orm.junit.Jpa;
22-
import org.hibernate.testing.orm.junit.SkipForDialect;
2323
import org.junit.jupiter.api.Test;
2424

2525
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -37,6 +37,7 @@ public class ConstraintInterpretationTest2 {
3737
}
3838
catch (ConstraintViolationException cve) {
3939
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
40+
// DB2 and Informix error messages don't contain the primary key constraint name
4041
if ( !(scope.getDialect() instanceof DB2Dialect) && !(scope.getDialect() instanceof InformixDialect) ) {
4142
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id" ) );
4243
}
@@ -63,6 +64,7 @@ public class ConstraintInterpretationTest2 {
6364
}
6465
catch (ConstraintViolationException cve) {
6566
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, cve.getKind() );
67+
// DB2 error message doesn't contain constraint or column name
6668
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
6769
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "name" ) );
6870
}
@@ -78,15 +80,14 @@ public class ConstraintInterpretationTest2 {
7880
}
7981
catch (ConstraintViolationException cve) {
8082
assertEquals( ConstraintViolationException.ConstraintKind.UNIQUE, cve.getKind() );
83+
// DB2 error message doesn't contain unique constraint name
8184
if ( !(scope.getDialect() instanceof DB2Dialect) ) {
8285
assertTrue( cve.getConstraintName().toLowerCase().contains( "ssnuk" ) );
8386
}
8487
}
8588
} );
8689
}
8790

88-
@SkipForDialect(dialectClass = InformixDialect.class,
89-
reason = "multi-column check constraints must be created using 'alter table', and we don't have a StandardCheckConstraintExporter")
9091
@Test void testCheck(EntityManagerFactoryScope scope) {
9192
scope.inTransaction( em -> {
9293
try {
@@ -95,7 +96,10 @@ public class ConstraintInterpretationTest2 {
9596
}
9697
catch (ConstraintViolationException cve) {
9798
assertEquals( ConstraintViolationException.ConstraintKind.CHECK, cve.getKind() );
98-
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "namecheck" ) );
99+
// CockroachDB error messages don't contain the check constraint name
100+
if ( !(scope.getDialect() instanceof CockroachDialect) ) {
101+
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "namecheck" ) );
102+
}
99103
}
100104
} );
101105
}
@@ -107,6 +111,7 @@ public class ConstraintInterpretationTest2 {
107111
}
108112
catch (ConstraintViolationException cve) {
109113
assertEquals( ConstraintViolationException.ConstraintKind.FOREIGN_KEY, cve.getKind() );
114+
// HANA error messages don't contain the foreign key constraint name
110115
if ( !(scope.getDialect() instanceof HANADialect) ) {
111116
assertTrue( cve.getConstraintName().toLowerCase().endsWith( "id2to1fk" ) );
112117
}

0 commit comments

Comments
 (0)